* 字符串中和转换功能相关的方法
* public byte[] getBytes():把字符串转换成字节数组
* public char[] toCharArray():把字符串转换成字符数组
* public static String valueOf(char[] chs):把字符数组转换成字符串
* public static String valueOf(int i):把int 类型转换成字符串类型
* public String toLowerCase():把字符串转换成小写
* public String toUpperCase():把字符串转换成大写
* public String concat(String str):拼接字符串,相当于"+"
* String 类中和替换功能相关的方法
* public String replace(char old,char new)将旧字符用新字符替换
* public String replace(String old,String new)将旧字符串用新字符串替换
* public string trim() 去掉两端空格
* public int compareTo(String str)按照字典顺序比较字符串
* public int compareToIgnoreCase(String str)不区分大小写按照字典顺序比较字符串
用一下方法
* 统计大字符串中小字符串出现的次数
* bigStr="woaijavawohenaijavawoahendeaijavawoahendehenaijavabuxinwoaijavadezaijian"
* smallStr="java"
* 定义一个计量变量count
* 循环,在大字符串中查找是否存在小字符串(indexOf),如果不存在跳出
* 如果存在count++,并且将以后的字符串截取(substring)赋值给bigStr,再进行查找
public class StringTest{
public static void main(String[] args) {
String bigStr="woaijavawohenaijavawoahendeaijavawoahendehenaijavabuxinwoaijavadezaijian";
String smallStr="java";
int count=0;
while(bigStr.indexOf(smallStr)!=-1){
count++;
//将以后的字符串截取(substring)赋值给bigStr
bigStr = bigStr.substring(bigStr.indexOf(smallStr)+1);
}
System.out.println(count);
}
}
很简单吧!!!
|