1 String类常用方法
public char charAt(int index);//返回字符串中的第index个字符
public int length();//返回字符串长度
public int indexOf(String str);//返回字符串中出现str的第一个位置
public int indexOf(String str,int fromIndex);//返回字符串中从fromIndex开始出现str的第一个位置
public boolean equalsIgnoreCase(String another);//比较字符串与another是否一样(忽略大小写)
public String replace(char oldChar,char newChar);//字符串用newChar替代oldChar
public boolean startWith(String prefix); //判断字符串是否以prefix字符串开头
public boolean endsWith(String suffix); //判断字符串是否以suffix字符串结尾
public String toUpperCase();//返回一个字符串为该字符串的大写形式
public String toLowerCase();//返回一个字符串为该字符串的小写形式
public String substring(int beginIndex,int endIndex);//返回该字符串从beginIndex开始到endIndex结尾的子字符串
public String trim();//返回将该字符串去掉开头和结尾空格后的字符串
public static String valueOf()将基本类型数据转换为字符串
public String[] split(String regex)将一个字符串按照指定的分隔符分隔,返回分隔后的字符串数组
public class TeatString {
public static void main(String[] args) {
String s = "AaaaABBBBcc&^%ADdfdgfdadfsdgfg99876 _haHA";
int lCount = 0, uCount = 0, oCount = 0;
for (int i=0;i<s.length();i++){
char c = s.charAt(i);
if(c >= 'a' && c<='z'){
lCount ++;
}else if (c>='A'&& c<= 'Z'){
uCount ++;
} else {
oCount++;
}
}
System.out.print(lCount + " "+ uCount + " "+ oCount+" ");
System.out.println();
String s1 = "sunjavahpjavaokjavajjavahahajavajavagoodjava";
String sToFind = "java";
int count = 0;
int index = -1;
while (s1.indexOf(sToFind) != -1){
index = s1.indexOf(sToFind);
s1= s1.substring(index+sToFind.length());
count++;
}
System.out.println(count);
}
}
、
2 StringBuffer类
java.lang.StringBuffer代表可变的字符序列
StringBuffer和String类似,但StringBuffer可以对其字符串进行改变