String Common Operations

Source: Internet
Author: User
1. Comparison string: Compare, compareto, equals, = ,! =
2. Positioning characters and substrings: startwith/endswith, indexof/lastindexof, indexofany/lastindexofany
3. format the string: Format
4. connection string: Concat, join, +
5. Split string: Split
6. insert and fill strings: insert, padleft/pagright
7. Delete and cut strings: Remove, TRIM/trimstart/trimend
8. Copy strings: Copy and copyto
9. Replace string: replace
10. Change the case sensitivity: toupper/tolower console. writeline ("----------------- test join ");
String STR = "";
String [] strarr = {"AAA", "BBB", "CCC "};
STR = string. Join ("+", strarr );
Console. writeline (STR); // STR = "AAA + BBB + CCC"

Console. writeline ("------------------- test format ");
String stra = "hello ";
String strb = "world ";
STR = string. Format ("{0}, {1 }! ", Stra, strb );
Console. writeline (STR );

Console. writeline ("------------------- test compareto ");
Console. writeline (stra. compareto (strb); //-1
Console. writeline ("ABC". compareto ("ABC"); // 0
Console. writeline ("ABC". compareto ("abcded"); //-1
Console. writeline ("ABCDE". compareto ("ABC"); // 1
Console. writeline (string. Compare ("ABC", "ABC"); // 0
Console. writeline (string. Compare ("abdddc", "ABC"); // 1
Console. writeline (string. Compare ("Abd", "abcdd"); //-1
Console. writeline ("------------------- test indexof ");
Console. WriteLine (str. IndexOf ('l '));
Char [] aaa = {'E', 'h', 'O '};
Console. WriteLine (str. IndexOfAny (aaa); // indexes of any character in aaa and the first matching item in str. The result is 0.
Console. WriteLine (str. LastIndexOfAny (aaa); // index of any character in aaa and the last matching item in str. The result is 1.
Console. WriteLine ("------------------- test Split ");
StrArr = str. Split (','); // The Split string is: strArr [0] = hello strArr [1] = world
Int I = 0;
While (I <strArr. Length)
{
Console. WriteLine (strArr [I]);
I ++;
}

Console. WriteLine ("------------------- test PadLeft ");
Str = "hello ";
Str = str. PadLeft (20, '*'); // 20 indicates the total length of the filled string.
Str = str. PadLeft (20 ,'*');
Console. WriteLine (str );

Console. WriteLine ("------------------- test Remove ");
Str = "abcdefg ";
Str = str. Remove (1, 3 );
Console. WriteLine (str );

Console. WriteLine ("------------------- test Trim ");
Str = "% hello & $ #";
Char [] trmchar = {'%', '&', '$ ','#'};
STR = Str. Trim (trmchar );
STR = Str. trimstart (trmchar); // only delete the starting trmchar value and the result is Hello & $ #
STR = Str. trimend (trmchar); // Delete only the trmchar value at the end, and the result is % Hello.
String str1 = string. Copy (STR );
Console. writeline (STR );

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.