#####indexof()
package day07Test;
/**
* 统计字符在句子中出现的次数
* @author gengyantao
*
*/
public class Demo1 {
public static void main(String[] args) {
int fromindex=0;
int index=0;
int count=0;
String subStr="I";
String str ="you can you up,I can I can say can";
while(index !=-1) {
index = str.indexOf(subStr,fromindex);
if (index !=-1) {
fromindex = subStr.length() + index;
count++;
}
}
System.out.println(count);
}
}
####
substing字符串截取
package day07Test;
/**
* substring字符串截取
* @author gengyantao
*
*/
public class domaindome {
public String subDomain(String ad) {
int beginindex=ad.indexOf(".");
int endindex=ad.indexOf(".", beginindex+1);
String name = ad.substring(beginindex+1, endindex);
return name;
}
public static void main(String[] args) {
String emarad="http://www.emar.com";
String baiduad="http://www.baidu.com";
domaindome domain = new domaindome();
String name1= domain.subDomain(emarad);
String name2= domain.subDomain(baiduad);
System.out.println(name1);
System.out.println(name2);
}
}
####
str.trim()//去除字符两边的空格
charAt(index)//获取下标的字符