Common uses of some string manipulations in C #

Source: Internet
Author: User

Overview

This article mainly explains the basic operation knowledge of string strings.

Reverse output

String str = Console.ReadLine ();            for (int i = str.) Length-1; I >= 0; i--)            {                console.write (str[i]);            }            Console.ReadLine ();

Calculate the length of a string

String myString = "This is a test!"; Console.WriteLine ("text is: {0}", myString) Console.WriteLine ("Text ' s long is: {0}", Mystring.length)

Convert case

myString = Mystring.tolower (); Convert all characters to lowercase myString = Mystring.toupper (); Convert all characters to uppercase

Delete space before and after

myString = Mystring.trim (); Delete the space before and after the string char[] TrimChars = {', ' e ', ' s '}; Ready to delete the character myString = Mystring.trim (trimChars); Delete all specified characters myString = Mystring.trimend (); Delete the string after the space myString = Mystring.trimstart (); Remove spaces before a string

Add a space

myString = Mystring.padright (14, "); When the string is not 14 bits long, fill myString = Mystring.padleft (14, ") with the specified character on his right; When the string is not 14 bits long, fill with the specified character on his left

Splitting a string

string[] Nstrs = Mystring.split (", 3); Split by Space and returns the first three strings

Get substring

String a = Mystring.substring (2,2); Gets two characters starting from the third bit of the mystring string, because the index start bit is 0

Replacing characters in a string

String a = Mystring.replace ("i", "O"); Replace All "I" in this string with "O"

String is a read-only group of char variables

String myString = "This is a test!"; foreach (char mychar in myString) {Console.Write ("{0}", MyChar);}

An array of characters that can be read and written

char[] Mychars = Mystring.tochararray ();

How to represent special characters

Since double quotation marks are used in C # to break the beginning and end of a string, for some special characters, such as double quotation marks, you need to use auxiliary characters called escape characters to represent them.

Summarize

String operations are particularly important in programming and are the most frequently used, and need to master the common use of string operations




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Common uses of some string manipulations in C #

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.