当前位置:网站首页>C # getting started series (12) -- string

C # getting started series (12) -- string

2022-06-12 09:30:00 InfoQ


String Overview

stay  C#  in , You can use character arrays to represent strings , however , A more common practice is to use  string  Keyword to declare a string variable .string  Keywords are  System.String  Alias of class .

String Class represents text , A series of Unicode character . In namespace system in .

String The members provided by the class do the following : Compare String object ; return String The index of a character or string within an object ; Copy String The value of the object ; Separate or combine strings ; Modify the value of the string ; The digital 、 The date and time or enumeration value is formatted as a string ; Normalize strings .


String common properties

String  Class has two properties :

  • Chars: Get current String The character at the specified character position in the object .
  • Length: Get current String The number of characters in the object .

class Program
{
 static void Main(string[] args)
 {
 string str = " I'm a string ";

 Console.WriteLine(str[0]); //  Output : I
 Console.WriteLine(str.Length); //  Output :7

 Console.ReadLine();
 }
}

String common methods

  • Compare Method
    :Compare There are many ways to overload , Implement two strings according to the specified rules 、 The comparison operation of the specified range , Method returns an integer , Used to describe the relative position in the sorting of two strings .
  • Concat Method
    :Concat There are many ways to overload , Realize the connection operation of the specified string or object .
  • Contains Method
    : Returns a Boolean value , This value represents the specified String Whether the object appears in this string .
  • EndsWith Method :
    : Returns a Boolean value , Determines whether the end of this string instance matches the specified string .
  • StartsWith Method :
    : Returns a Boolean value , To determine whether the string starts with the specified substring .
  • Equals Method
    :Equals Methods can be overloaded in many ways , The implementation determines two according to certain rules String Whether the instance has the same value .
  • Format Method
    :Format Methods can be overloaded in many ways , Replaces one or more format items in the specified string with the string representation of the specified object .
  • IndexOf Method
    :IndexOf Methods can be overloaded in many ways , Reports the index of the first occurrence of a specified character or string within a specified range in this instance .
  • and 8 A similar approach is :IndexOfAny,LastIndexOf, LastIndexOfAny Other methods , Reports the index of a match of a specified character or string within a specified range in this instance .
  • Insert Method
    :Insert Method inserts a specified index at the specified index position in this instance String example .
  • Remove Method
    :Remove Method to delete the character specified in this instance .
  • **Replace Method :**Replace Method to replace the character or substring specified in this instance .
  • **Split Method :**Split There are many ways to overload , Realize the function of dividing the string according to the specified characters to form multiple substrings .
  • Substring Method
    :Substring There are two ways to overload , Realize the function of substring the string according to the specified starting position and specified length .
  • ToLower Methods and ToUpper Method
    : Converts characters in a string to lowercase , Or convert to uppercase .
  • Trim Method
    : Delete all leading and trailing white space characters in the string . There is also “TrimStart” and “TrimEnd”  They are the blank characters before and after deleting the string
原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206120926161081.html