public class SubstringExample {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = new String("Javacodegeeks");
System.out.println("Initial string is: " + str);
System.out.println("Start position=4 and no end position specified: "
+ str.substring(4));
System.out.println("Start position=2 and end position=11: "
+ str.substring(2, 11));
// if start = end, then the extracted string will be empty
System.out.println("Start position=3 and end position=3: "
+ str.substring(3, 3).isEmpty());
// In the following cases we will get
// java.lang.StringIndexOutOfBoundException
// if (start < 0 or end < 0)
System.out.println("Start position=-2 and end position=5: "
+ str.substring(-2, 5));
// if (start > end)
System.out.println("Start position=5 and end position=2: "
+ str.substring(5, 2));
}
}
原文:
http://examples.javacodegeeks.com/java-basics/string-java-basics/substring-java-example/
子串substring
最新推荐文章于 2022-10-09 21:34:01 发布