C # basic string processing

Source: Internet
Author: User
    • Locate string
      String STR = "ABCD ";
      Int M1 = Str. indexof ("B ");// M1 = 1
      Int m2 = Str. indexof ("cd ");// M2 = 2
      Int m3 = Str. indexof ("");// M3 = 0
      Int M4 = Str. indexof ("W ");// M4 =-1

 

  • Format a string
    String str1 = string. Format ("(c) Currency: {0: c} \ n",-123.45678f );
    // Format it to the currency type; str1 = (c) Currency: ¥-123.46
    String str1 = string. Format ("(d) Short Date: {0: d} \ n", datetime. Now );
    // Format it to the time type; str1 = (d) Short Date:

    Character

    Description

    Example

    Output

    C Currency String. Format ("{0: C3}", 2) $2.000
    D Decimal String. Format ("{0: D3}", 2) 002
    E Scientific notation 1.20e + 001 1.20e + 001
    G General String. Format ("{0: g}", 2) 2
    N Number separated by semicolons String. Format ("{0: n}", 250000) 250,000.00
    X Hexadecimal String. Format ("{0: x000}", 12) C
    String. Format ("{0: 000. 000}", 12.2) 012.200

 

    • Truncate string
      String STR = "Hello world! ";
      String str1 = Str. substring (0, 5 );// Str1 = "hello ";

 

    • Comparison string
      // Compareto (): If the value of the parameter is equal to that of the instance, 0 is returned. If the value of this parameter is greater than the value of the parameter, 1 is returned. Otherwise,-1 is returned.
      String str1 = "ABC ";
      String M1 = str1.compareto ("ABC ");// M1 = 0
      String m2 = str1.compareto ("AB ");// M2 = 1
      String m3 = str1.compareto ("ABCD ");// M3 =-1
      // Equals (): If the parameter value is the same as that of the instance, the parameter value is true. Otherwise, the parameter value is false.
      String str1 = "ABC", str2 = "ABC", str3 = "ABC ";
      Bool b1 = str1.equals (str2 );// B1 = false
      Bool b2 = str1.equals (str3 );// B2 = true

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.