标签:uil ack tchar tin stl com junit pre his
package day01; import org.junit.Test; public class TestString { /** * 测试常量池 * * */ @Test public void testConstantPool(){ String str1 = "Hello"; // String str2 = "Hello"; //两个对象使用==进行比较,返回值为true 说明两个对象引用 指向的内存区域相同 System.out.println(str1 == str2); String str3 = new String("Hello"); //说明两个对象引用 指向的内存区域 不 相同,使用new创建的字符串 不会缓存在String //常量池 System.out.println(str1 == str3); } /* * 获取String 对象的长度 */ @Test public void testLength(){ String str1 = "Hello"; System.out.println(str1.length()); //在内存中采用Unicode编码 每个字节2个字符 //任何一个字符都算一个长度 String str2 = "你好,String"; System.out.println(str2.length()); } /* * 字符串截取 */ @Test public void testSubstring(){ String str = "http://www.oracle.com"; String substr = str.substring(11, 17); System.out.println(substr); } /* * 去掉空格Trim * */ @Test public void testTrim(){ String userName = " good man"; System.out.println(userName.length()); userName = userName.trim(); System.out.println(userName.length()); System.out.println(userName); } /* * 遍历字符串中的字符序列 */ @Test public void testCharAt(){ String name = "xuejingbo"; for (int i=0;i<name.length();i++){ char c = name.charAt(i); System.out.print(c+ " "); } } //endWith 以特定字符结束 startsWidth 以**开始 //toLowerCase //toUpperCase //将其他类型的转为字符串 //String 类的valueOf 重载的方法,可以将double类型,int类型,Boolean及char //类型转变为String类变量 @Test public void testValueOf(){ double pi = 3.1415926; int value = 123; boolean flag = true; char[] charArr = {‘a‘,‘b‘,‘c‘,‘d‘,‘e‘,‘f‘,‘g‘}; String str = String.valueOf(pi); System.out.println(str); str = String.valueOf(value); System.out.println(str); } /* * 测试StringBuilder append */ @Test public void testAppend(){ StringBuilder sb = new StringBuilder("programing language:"); sb.append("java").append("cpp").append("php").append("c#"); System.out.println(sb.toString()); } /* * 测试StringBuilder insert */ @Test public void testInsert(){ StringBuilder sn = new StringBuilder("thisisatest"); sn.insert(5, "html"); System.out.println(sn); } }
标签:uil ack tchar tin stl com junit pre his
原文地址:http://www.cnblogs.com/vali/p/7615755.html