String manipulation in Java

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.