C # string common function exercises

Source: Internet
Author: User
Code
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;

Namespace CSharp
{
Public class TestString
{
Public TestString ()
{
String [] sA = new string [5] {"H", "e", "l", "l", "o "};
Console. WriteLine (String. Concat (sA ));
Console. WriteLine (String. Join ("-", sA ));

String s = String. Join ("", sA );
Console. WriteLine (s );

Char [] c = s. ToCharArray ();

String sl = s. ToLower ();
String su = s. ToUpper ();

Console. WriteLine (sl + "--" + su );
String s2 = s. PadLeft (10 ,'_');
Console. WriteLine (s2 );

String s3 = s. PadRight (10 ,'_');
Console. WriteLine (s3 );

String s4 = String. Join ("-", sA );
String [] s5 = s4.Split (new char [] {'-'});
Foreach (string s6 in s5)
{
Console. WriteLine (s6 );
}

String s7 = s2.Trim ();
Console. WriteLine (s7 );
String s8 = s2.Trim (new char [] {'_'});
Console. WriteLine (s8 );

// TrimEnd, TrimStart is the same.

If (s2.StartsWith ("H "))
{
Console. WriteLine ("S2 was start with 'h '");
}
Else
{
Console. WriteLine ("S2 wasn't start with 'h '");
}

If (s2.EndsWith ("o "))
{
Console. WriteLine ("S2 was end with 'O '");
}
Else
{
Console. WriteLine ("S2 wasn't end with 'O '");
}

Console. WriteLine (s2.Substring (5, s2.Length-5 ));
Console. WriteLine (s2.Remove (0, 5 ));

Console. WriteLine (s2.Replace ("l", "I "));

If (String. IsNullOrEmpty (s2 ))
{
Console. WriteLine ("S2 Is NullOrEmpty ");
}
Else
{
Console. WriteLine ("S2 isn' t NullOrEmpty ");
}

}
}
}

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.