首页 > 代码库 > java-测试开发字符串

java-测试开发字符串

package j2se;

public class StringDemo {
	private String demoString="ceshixiaoyouning";
	
	public void testString(){
		String dsg=demoString.concat("very beautiful");//字符串相加,也可以用+来表示
		System.out.println(dsg);
		
		int len=demoString.length();//字符串的长度
		System.out.println(len);
		
		boolean eq="ceshixiaoyouning".equals(demoString);//比较两个字符串是否相等
		System.out.println(eq);
		
		String sub=demoString.substring(5, 8);//取子字符串,从第5个字符开始,到底8个字符,但是不包含第8个字符
		System.out.println(sub);
		
		String subString=demoString.substring(5);//取子字符串,从第5个字符开始一直到字符串尾
		System.out.println(subString);
		
		boolean sw =demoString.startsWith("ceshi");//判断是否以某个字符串开头
		System.out.println(sw);
		
		boolean ew=demoString.endsWith("youning");//判断石头以某个字符串结尾
		System.out.println(ew);
		
		int subIndex=demoString.indexOf("ce");//找出子字符串在字符串中第一次出现的index,如果找不到则返回-1
		System.out.println(subIndex);
		
		int lastIndex=demoString.lastIndexOf("i");//找出子字符串在字符串最后一次出现的index,如果找不到则返回-1
		System.out.println(lastIndex);
		
		System.out.println(demoString.toUpperCase());//字符串中的字全变成大写
		System.out.println(demoString.toLowerCase());//字符串中的字全变成小写
		System.out.println("    youning   ".trim());//将字符串首尾的空格去掉
		
		String subReplace=demoString.replace("ceshi", "hello");//将字符串中的某段字符串替换成新的字符串
		System.out.println(subReplace);
		
		String subReplaceF=demoString.replaceFirst("i", "hhh");//将字符串中第一次出现的子字符串替换成新的字符串,支持正则
		System.out.println(subReplaceF);
		
		String subReplaceA=demoString.replaceAll("i", "hhh");//将字符串中出现的所有子字符串替换成新的子字符串,支持正则
		System.out.println(subReplaceA);
	}
	
	public static void main(String[] args) {
		StringDemo s=new StringDemo();
		s.testString();
	}
}

 运行结果:

ceshixiaoyouningvery beautiful
16
true
xia
xiaoyouning
true
true
0
13
CESHIXIAOYOUNING
ceshixiaoyouning
youning
helloxiaoyouning
ceshhhhxiaoyouning
ceshhhhxhhhaoyounhhhng

java-测试开发字符串