操作字符串,输出结果如附件所示。源程序代码如下: //程序文件名为TestString.java public class TestString { public static void main(String[] args) { String str = new String("The substring begins at the specified beginIndex."); String str1 = new String("string"); String str2 = new String(); int size = str.length();//字符串长度 int flag = str.indexOf("substring"); str2 = str.substring(flag,flag + 9);//取子字符串 System.out.println("字符串" + str + "\n总长度为:" + size); if(str1.equals(str2))//判断是否相等
System.out.println("截取的字符串为:" + str1);
else System.out.println("截取的字符串为:" + str2); } } |