子串substring

来源:互联网 发布:电子病历源码 编辑:程序博客网 时间:2024/04/29 11:00
public class SubstringExample {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubString 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 emptySystem.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/
0 0
原创粉丝点击