Java Learning Summary: elegant strings

Source: Internet
Author: User

Java Learning: A flowing string


I believe that regardless of whether we use the Java language to develop a project or data analysis processing, we have to apply the string-related processing methods. This society has a string-related shadow everywhere: logs, documents, books, and so on. Since we can't live without strings, let's get to know each other.

how to construct a string?

In the Java language, there are two ways to create a string object: One is to use a string literal

1 string message=new string ("Welcome to Java"); 2 String message2= "Welcome to Java";

The second is through the character array.

1 Char [] array={' G ', ' o ', ' o ', ' d ', ' ', ' d ', ' a ', ' Y '}; 2 string message3=new string (array);

Note : A string variable stores a reference to a string object that is stored in a string object. So strictly speaking, the term string variables, string objects, and string values are different, but usually we are collectively referred to as strings for the sake of simplicity.

immutable? string so drag!

The String object is immutable, and its contents cannot be changed. For example, the following two lines of code:

1 String s= "Java"; 2 s= "HTML";

The first statement creates a String object with the content "Java" and assigns its reference to s. The second statement creates anew String object with the content "HTML" and assigns it to s. So after executing these two statements, two String objects are unchanged, and the contents of them are unchanged, but the s variable finally points to the new String object.

Comparison of Strings

== == can only detect strings string1 and string2 does point to the same object, but it doesn't tell us if their content is the same. So java Inside to determine if the strings are the same, we should use equals method or compareto method.

1 string s1=new string ("Welcome to Java"); 2 String s2= "Welcome to Java"; 3 String s3= "Welcome to C + +"; 4 String s4= "Welcome to Java"; 5 System.out.println (s1.equals (S2)); 6 System.out.println (S1.equals (S3)); 7 System.out.println (S1.compareto (S2));

Add: When comparing two strings for equality, methods equalsignorecase and comparetoignorecase can ignore the case of letters and then compare them.

1 System.out.println (S1.equalsignorecase (S4)); 2 System.out.println (S2.comparetoignorecase (S4));

string lengths, characters, and combined strings

length Yes string s s.length () s.charat (index) can be used to extract strings s a specific character, where subscript index 0 to s.length ()-1

In addition, we can use the concat method to concatenate two of strings. The string s1 and s2 connections form the S3, as shown in the following code.

1 String ss= "Baiyi"; 2 String ss2= "Shaonian"; 3 String ss3=ss.concat (SS2); 4 System.out.println (SS3);

Of course, it seems that we tend to prefer a simpler approach, which is to use the plus sign (+) directly to concatenate two or more strings to form a new string.

How do I get a substring?

As mentioned earlier, we can get any single character of a string by means of the CharAt method, but what should we do to get its substring? We'll use the substring method.

1 System.out.println ("Welcome to Java". Substring (0,11) + "C + +");
string conversion, substitution and separation methods, learn ing ...

toLowerCase: Returns a new string after converting all characters to lowercase

toUpperCase: Returns a new string after converting all characters to uppercase

Trim: Returns a new string after removing whitespace characters from both ends

Replace: Returns a new substring that replaces all of the characters in the string with its matching character

Replacefirst: Returns a new substring that replaces the first one in the string with the substring after it matches

ReplaceAll: Returns a new substring that replaces all of the strings in this string with the substring after it matches

Split: Returns a string array of substrings separated by delimiters

1System.out.println ("Welcome to". toLowerCase ());2System.out.println ("Welcome to". toUpperCase ());3System.out.println ("Welcome to". Trim ());4System.out.println ("Welcome to". Replace (' e ', ' A '));5System.out.println ("Welcome to". Replacefirst ("E", "ABC"));6System.out.println ("Welcome to". ReplaceAll ("E", "AB")));7         8String[] str= "java#html#c++". Split ("#", 0);9  for(inti=0; i<str.length; i++) {TenSystem.out.println (Str[i] + ""); One } AString[] str2= "java#html&c++". Split ("[#&]", 0); -  for(inti=0; i<str2.length; i++) { -System.out.println (Str2[i] + ""); the}

Java Learning Summary: elegant strings

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: 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.