String manipulation--cut, intercept, replace, find, compare, go to space ...

Source: Internet
Author: User
string concatenation

Direct with +: String a = "I"; String B = "Love"; String c = "You"; String d = a+b+c; I'm going to get love for you.

"I" + "Love" + "You" Get is also I love youString Comparisonsboth = = and equals can compare strings and return all Boolean type string a = "I"; String B = "Love"; A==b returns false A.equals (b) returns false = = Compares 2 strings to the same address, equals () compares the actual strings, such as case, length, etc.
is included1, contains (); The return value is Boolean type String a = "SZRFRRGDHJD"; A.contains ("G") contains, so returns True
2, IndexOf (); Returns an int type, usually with a String a = "Qwertyu" with substring (); A.indexof ("E"); He returns 2 of the type int, which means that E is in the 2nd position in the string A. If there are multiple E, the position of the first E is always returned.
is empty1, = null; 2, IsEmpty (), the return is Boolean String a = "sssss";  A.idempty (); A is not empty and returns false
Split StringThe split () split string returns a string array of string a = "Abcdeafghijk"; string [] B = a.split ("a");//A as the split point, a division of strings a of 2 string arrays is b[0] = "ABCE"; B[1] = "Efghijk" If the string contains several a, string a = "Abcdeafghaijk";  String [] b = a.split ("a"); With each A as the dividing point, get b[0] = "Abde"; B[1] = "FGH"; B[2] = "Ijk";
And so on, and so forth.
intercept the middle segment of a stringThe substring (int start,int end) intercepts the string from the middle of the start to ends, or it can only pass in an int start intercept from start to the bottom of an actual operation:

{name= Duan Lian, age=25, sex= Male, id=12, hobby= eat. Sleep}  to get the number of ID is the value, that is, the type of int 12
String s = "{name= Duan Lian, age=25, sex= Male, id=12, hobby= eat." Sleep} ";
		 if (S.contains ("id")) {
           //Get ID field is in the string ordinal number position
           int start = S.indexof ("id");
           The "id" field is followed by the "Hobby" field, and the position of the Hobby field
           int end = S.indexof ("hobby");
            * * {name= Duan Lian, age=25, sex= Male, id=12, hobby= eat. Sleep}
            * start + 3: Starting from I + 3 where the ID is the beginning of the 12,
            * End-2:hobby-2 is exactly where the 12 ends; minus an h and a comma/
           String a = S.substring (start + 3, end-2);
           When ID is converted to int type
          int id = integer.parseint (a);
The ID you get is 12.

string Substitution
Replace (Oldchar, Newchar) method	parameter 1: The character to be replaced, parameter 2: the character to be replaced
The purpose of this method is to replace all the specified characters in the string and then generate a new string. After this method call, the original string does not change. For example:
String s = "ABCDE8FGHIJK8LMN";
String a = s.replace (' 8 ', ' Q ');
The value of a is "ABCDEQFGHIJKQLMN"
ReplaceAll (string regularexpression, String replacement),	replacing all contained strings
Parameter 1: The string to be replaced, 2, the string to be replaced by the strings s = "QQQQABCWWWABCGGGGABC";
String a = S.replaceall ("abc", "PPP");
The value of a is "QQQQPPPWWWPPPGGGGPPP"
If only the first ABC is replaced with Replacefirst () String s = "QQQQABCWWWABCGGGGABC";
String a = S.replacefirst ("abc", "PPP");
The value of a is "QQQQPPPWWWABCGGGGABC"

Go to SpaceThe difference between LTrim (), RTrim () and trim () functions
Returns a string without leading spaces (LTRIM), subsequent spaces (RTRIM), or leading and trailing spaces (trim)




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.