在JAVA语言中,字符串数据实际上由String类所实现的。Java字符串类分为两类:一类是在程序中不会被改变长度的不变字符串;二类是在程序中会被改变长度的可变字符串。Java环境为了存储和维护这两类字符串提供了 String和StringBuffer两个类。
String的一些常用操作
<1>字符串创建
String str="This is a String";
或者
String str=new String("This is a String");
<2>字符串长度
String str= "This is a String";
int len =str.length();
<3>指定字符或子字符串的位置
String str="Thisis a String";
Int index1 =str.indexOf("i"); //2
Int index2=str.lastIndexOf("i"); //12
<4>判断两个字符串是否相等
String str="This is a String";
Boolean result=str.equals("This is another String");
<5>得到指定位置的字符
String str="This is a String";
char chr=str.charAt(3);
<6>截取子字符串
str=str.substring(int beginIndex);
截取掉str从首字母起长度为beginIndex的字符串,将剩余字符串赋值给str;
str=str.substring(int beginIndex,int endIndex);
<6>字符串合并
String str="This is a String";
String str1=str.concat("Test"); //str1="This is a String Test"