Package Day01;import Org.junit.test;public class TestString {/** * Test constant POOL * * */@Testpublic void Testconstantpool () {String str1 = "Hello";//string str2 = "Hello";//two objects are compared using = =, and a return value of TRUE indicates that the two object references refer to the same memory area System.out.println (str1 = = str2); String str3 = new String ("Hello"),//indicates that the two object references point to a different memory region, and the string created with new is not cached in the string//constant pool System.out.println (str1 = = STR3);} /* Gets the length of the String object */@Testpublic void Testlength () {String str1 = "Hello"; System.out.println (Str1.length ());//Unicode encoded in memory each byte 2 characters//any one character counts a length of string str2 = "Hello, string"; System.out.println (Str2.length ());} /* * String intercept */@Testpublic void testsubstring () {string str = "http://www.oracle.com"; String substr = str.substring (11, 17); System.out.println (substr);} /* * Remove space trim * */@Testpublic void Testtrim () {String userName = "Good Man"; System.out.println (Username.length ()); userName = Username.trim (); System.out.println (Username.length ()); System.out.println (userName);} /* * traverse the character sequence in the string */@Testpublic void Testcharat () {string name = "Xuejingbo"; for (int i=0;i<name.length (); i++) {char c = name.charat (i); System.out.print (c+ "");}} Endwith a method that ends startswidth with a specific character and starts//tolowercase//touppercase//the valueof overloads of other types into a string//string class, you can set the double type, Type int, Boolean and char//types are converted to string class variables @testpublic void Testvalueof () {Double pi = 3.1415926;int value = 123;boolean flag = Tru E;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);} /* * Test StringBuilder append */@Testpublic void Testappend () {StringBuilder sb = new StringBuilder ("Programing language:"); Sb.append ("Java"). Append ("CPP"). Append ("PHP"). Append ("C #"); System.out.println (Sb.tostring ());} /* * Test StringBuilder Insert */@Testpublic void Testinsert () {StringBuilder sn = new StringBuilder ("Thisisatest"); Sn.insert (5, "html"); SYSTEM.OUT.PRINTLN (SN);}}
String manipulation in Java