Simple Method and example of a string

Source: Internet
Author: User

Split Splits a string into a group of arrays to traverse the Array
Example: string s1 = "abc, 123, lcf, I am Chinese ";
String [] items = s1.Split (',');
Foreach (string item in items)
{
Console. Write (item + "");
}
The above code will be output: abc 123 lcf I am a Chinese
 
Substring is used to intercept a string. The value of this parameter indicates to intercept the returned string from this parameter.
Example: string s1 = "February 1234 ";
S1 = s1.Substring (2 );
Console. WriteLine (s1 );
The above code will be output :,
 
 
IndexOf is used to obtain the position of this parameter value in this string, and a value is returned.
Example: string s1 = "February 1234 ";
Int i1 = s1.IndexOf ("4 ");
Console. WriteLine (i1 );
The above code will be output: 3
 
Contains: returns a Boolean value if the string Contains this parameter value.
Example: string s1 = "February 1234 ";
Bool i1 = s1.Contains ("2 ");
Console. WriteLine (i1 );
The above code will output: True
 
The CompareTo parameter value is compared with the string. If the value is identical, 0 is returned, and 1 is returned.
Example 1: string s1 = "February 5, 8 ";
Int i1 = s1.CompareTo ("August 8 ");
Console. WriteLine (i1 );
Example 2: Console. WriteLine ("Enter Password :");
String s1 = Console. ReadLine ();
Console. WriteLine ("Confirm Password :");
String s2 = Console. ReadLine ();;
Int i1 = s2.CompareTo (s1 );
While (true)
{
If (i1 = 0)
{
Console. WriteLine ("the two inputs are consistent! ");
Return;
}
Else
{
Console. WriteLine ("no two inputs! Enter ") again ");
S1 = Console. ReadLine ();
Console. WriteLine ("Enter again :");
S2 = Console. ReadLine ();
I1 = s2.CompareTo (s1 );
}
}
 
 
If the Equals parameter is the same as the string, True is returned, and False is returned if the Equals parameter is different.
Example: string s1 = "People's Republic of China ";
String s2 = "Republic of China ";
Bool B = s1.Equals (s2 );
Console. WriteLine (B );
The above code returns: False
 
 
Insert a character into the string. The parameter value must be the start position, and then enter the inserted content.
Example: string s1 = "People's Republic of China ";
S1 = s1.Insert (0, "medium ");
Console. WriteLine (s1 );
Return the code above: People's Republic of China
 
 
 
Remove the character after the specified position
Example: string s1 = "People's Republic of China ";
S1 = s1.Remove (3 );
Console. WriteLine (s1)
Return the code above: Republic of China
 
 
 
Replace
Example: string s1 = "People's Republic of China ";
S1 = s1.Replace (" ","");
Console. WriteLine (s1 );
Return the code above: People's Republic of China
 
 
StartsWith (EndsWith) determines whether the pre-(post) part of the string matches the Parameter
For example, string s1 = http://www.bkjia.com;
Bool B = s1.StartsWith ("http ://");
Bool c = s1.EndsWith (". com ");
Console. WriteLine (B );
Console. WriteLine (c );
The preceding Code returns True False.
 
 
Trim (TrimStart) (TrimEnd) clears the spaces (characters) at the beginning and end of the string)
Example: string s1 = "http://www.bkjia.com ";
String s2 = s1.Trim ();
String s3 = s1.TrimStart (new char [] {'h', 't', 'w '});
String s4 = s1.TrimEnd (new char [] {'', 'M '});
Console. WriteLine (s2 + "separated ");
Console. WriteLine (s3 );
Console. WriteLine (s4 );
The above code returns separately: http://www.bkjia.com separated



From Yidu

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.