C # operation string Summary

Source: Internet
Author: User

C # operation string Summary

C # operation string Summary

String stringMessage = string. Empty;
StringMessage. ToLower () to lowercase letters
StringMessage. ToUpper () is converted into uppercase letters
StringMessage. Trim () removes leading and trailing Spaces
StringMessage. Trim (trimChars) delete other characters
StringMessage. TrimStart () space before deletion
StringMessage. TrimEnd () space after deletion
StringMessage. PadLeft (10) adds spaces on the left to extend the string length.
StringMessage. PadRight (10) adds spaces on the right to extend the string length.
StringMessage. PadX (10, '-') adds other characters to make the string reach a certain length. X indicates Left/Right.
String [] messages = stringMessage. Split ('') Splits a string into arrays based on a specific character

Here, the Split () method is commonly used, for example:
String testString = "aaa, bbb, ccc, ddd ";
String [] testMessages = testString. Split (',');
TestMessages [0] = "aaa ";
TestMessages [1] = "bbb ";
TestMessages [2] = "ccc ";
TestMessages [3] = "ddd ";

String SEARCH:
Use the Indexof () and LastIndexof () methods to determine the index of the substring and then intercept it.

We can also implement the IFormattable interface to define our own string format class.
Public class MyFormatClass: IFormattable
{
Public double x, y, z;
Public Vector (double dx, double dy, double dz)
{
X = dx;
Y = dy;
Z = dz;
}
Public string ToString (string format, IFormatProvider formatProvider)
{
If (format = null)
Return ToString ();
String formatUpper = format. ToUpper ();
Switch (formatUpper)
{
Case "N ":
Return "|" + Norm (). ToString () + "| ";
Case "VE ":
Return String. Format ("({0: E}, {1: E}, {2: E})", x, y, z );
Case "IJK ":
StringBuilder sb = new StringBuilder (x. ToString (), 30 );
Sb. Append ("I + ");
Sb. Append (y. ToString ());
Sb. Append ("j + ");
Sb. Append (z. ToString ());
Sb. Append ("k ");
Return sb. ToString ();
Default:
Return ToString ();
}

}
Public override string ToString ()
{
Return "(" + x + "," + y + "," + z + ")";
}
Public double Norm ()
{
Return x * x + y * y + z * z;
}
}

Use regular expressions to control several common Regular Expressions in string format:

Internet address expression: http: // ([/w-] +/.) + [/w-] + (/[/w -./? % & =] *)?
Email Address expression:/w + ([-+.] /w +) * @/w + ([-.] /w + )*/. /w + ([-.] /w + )*
Zip code:/d {6}
Phone number :(/(/d {3}/) |/d {3 }-)? /D {8}

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.