String (String) in C #

In C #, you can use strings (strings) as array of characters. However, it is more common to use string keywords to declare a string variable. The string keyword is an alias for the System.String class in C #.

In C #, you can use strings as arrays of characters or use string keywords to declare a string variable. The string keyword is an alias for the System.String class in C #.

Create a String object in C #

You can create String objects using one of the following methods:

  1. Assign a string constant to a String variable
  2. Use a constructor of String class
  3. Use string concatenation operator (+)
  4. Obtain an attribute or call a method that returns a string
  5. Call a format method to convert a value or an object to its string representation.

The following example illustrates the methods for creating a string in C #:

 using System ; namespace QTMCsharp { class TestCsharp { static void Main ( string [] args ) { Console . WriteLine ( "Cac cach tao chuoi trong C#" ); Console . WriteLine ( "-------------------------------------" ); //su dung phep gan hang chuoi va toan tu noi chuoi string fname , lname ; fname = "Quản Trị" ; lname = "Mạng" ; string fullname = fname + " " + lname ; Console . WriteLine ( "Ho va ten: {0}" , fullname ); //su dung constructor cua lop string char [] letters = { 'H' , 'e' , 'l' , 'l' , 'o' }; string greetings = new string ( letters ); Console . WriteLine ( "nLoi chao bang tieng Anh: {0}" , greetings ); //tu cac phuong thuc ma tra ve mot chuoi string [] sarray = { "QTM" , "xin" , "chao" , "cac" , "ban" }; string message = String . Join ( " " , sarray ); Console . WriteLine ( "nThong diep: {0}" , message ); //dinh dang phuong thuc de chuyen doi mot gia tri DateTime waiting = new DateTime ( 2016 , 8 , 1 , 17 , 58 , 1 ); string chat = String . Format ( "Thong diep duoc gui luc {0:t} ngay {0:D}" , waiting ); Console . WriteLine ( "nThong diep: {0}" , chat ); Console . ReadKey (); } } } 

If you do not use the Console.ReadKey () command; then the program will run and finish (so fast that you can not see the results). This command allows us to see the results more clearly.

Compiling and running the above C # program will produce the following results:

String (String) in C # Picture 1

Properties of String class in C #

String class in C # has two properties:

  1. Chars: Get the Char object at a specific location in the current String object
  2. Length: Get the number of characters of the current String object

Method of String class in C #

The String class has several methods that are useful to you while working with String objects in C #. The following table lists the most commonly used methods:

public static int Compare (string strA, string strB): Compares two specific String objects and returns an integer that indicates their relative location in the sort order.

public static int Compare (string strA, string strB, bool ignoreCase): Compare two specific String objects and return an integer that indicates their relative location in the sort order. However, it ignores the type discrimination if the Boolean parameter is true.

public static string Concat (string str0, string str1): Connect a string of two String objects.

public static string Concat (string str0, string str1, string str2): Sequence of three String objects.

public static string Concat (string str0, string str1, string str2, string str3): Stringing four String objects.

public bool Contains (string value): Returns a value indicating whether or not the String object appears inside this string.

public static string Copy (string str): Create a new String object with the same value as the given string.

public void CopyTo (int sourceIndex, char [] destination, int destinationIndex, int count): Copy some specific characters from a given position of the String object to a specified position in an array of characters Unicode.

public bool EndsWith (string value): Determine whether or not the end of the String object is matched with the given string.

public bool Equals (string value): Determine whether or not the current String object and String object have the same value.

public static bool Equals (string a, string b): Determine whether or not two String objects have the same value.

public static string Format (string format, Object arg0): Replace one or more formatting entries in a given string with the string representation of a specific object.

public int IndexOf (char value): Returns the index (based on 0) for the first occurrence of the given Unicode character in the current string.

public int IndexOf (string value): Returns the index (based on 0) for the first occurrence of the given string in this Instance.

public int IndexOf (char value, int startIndex): Returns the index (based on 0) for the first occurrence of the given Unicode character in this string, starts the search at the position of the given character .

public int IndexOf (string value, int startIndex): Returns the index (based on 0) for the first occurrence of the given string in this Instance, starting the search at the position of the signature self gave.

public int IndexOfAny (char [] anyOf): Returns the index (based on 0) for the first occurrence of this Instance of any character in a specified Unicode character array .

public int IndexOfAny (char [] anyOf, int startIndex): Returns the index (based on 0) for the first occurrence of this Instance of any character in an Unicode character array specified, start searching at the position of the given character.

public string Insert (int startIndex, string value): Returns a new string where a given string is inserted at an indexed position in the current String object.

public static bool IsNullOrEmpty (string value): Indicates whether or not the given string is null or an Empty string.

public static string Join (string separator, params string [] value): Append a string of all elements of a string array, using a given Separator between each element.

public static string Join (string separator, string [] value, int startIndex, int count): Connect the sequence of defined elements of a string array, using the given Separator between each element.

public static string Join (string separator, string [] value, int startIndex, int count): Connect the sequence of defined elements of a string array, using the given Separator between each element.

public int LastIndexOf (char value): Returns the index (based on 0) for the last occurrence of the Unicode character given within the current String object.

public int LastIndexOf (string value): Returns the index (based on 0) for the last occurrence of a given string within the current String object.

public string Remove (int startIndex): Remove all current Instance characters, start at the specified location and continue to the last position, and return that string.

public string Remove (int startIndex, int count): Removes the number of characters given in the current string starting at a specified location and returns that string.

public string Replace (char oldChar, char newChar): Replace all given Unicode characters that appear in the current String object with the specified Unicode character and return the new string.

Public string Replace (string oldValue, string newValue): Replace all strings that appear in the current String object with the specified string object and return the new string.

public string [] Split (params char [] separator): Returns an array of strings that contain additional strings in the current String object, limited by elements of a given Unicode character array.

public string [] Split (char [] separator, int count): Returns an array of strings that contain sub-strings in the current String object, limited by elements of a given Unicode character array. The int parameter specifies the maximum number of extra strings to return.

public bool StartsWith (string value): Determine whether or not the start of the instance of this string matches the given string.

public char [] ToCharArray (): Returns an array of Unicode characters with all characters in the current String object.

public char [] ToCharArray (int startIndex, int length): Returns an array of Unicode characters with all characters in the current String object, starting from the specified index and to the given length.

public string ToLower (): Returns a copy of this string that has been converted to lowercase.

public string ToUpper (): Returns a copy of this string that has been converted to uppercase.

public string Trim (): Remove all whitespace characters from the current String object.

You can go to the MSDN library to get a complete list of methods and constructors of the String class.

String example in C #

The following example illustrates some of the above methods:

Comparing strings in C #:

 using System ; namespace QTMCsharp { class TestCsharp { static void Main ( string [] args ) { Console . WriteLine ( "So sanh chuoi trong C#" ); Console . WriteLine ( "-------------------------------------" ); string str1 = "So sanh chuoi trong C#" ; string str2 = "So sanh chuoi trong Csharp" ; if ( String . Compare ( str1 , str2 ) == 0 ) { Console . WriteLine ( str1 + " va " + str2 + " la giong nhau." ); } else { Console . WriteLine ( str1 + " va " + str2 + " la khong giong nhau." ); } Console . ReadKey (); } } } 

If you do not use the Console.ReadKey () command; then the program will run and finish (so fast that you can not see the results). This command allows us to see the results more clearly.

Compiling and running the above C # program will produce the following results:

String (String) in C # Picture 2

Check the substring in C #:

 using System ; namespace QTMCsharp { class TestCsharp { static void Main ( string [] args ) { Console . WriteLine ( "Kiem tra chuoi con trong C#" ); Console . WriteLine ( "-------------------------------------" ); string str = "Chuoi con trong C#" ; if ( str . Contains ( "trong" )) { Console . WriteLine ( "Tim thay chuoi con 'trong'." ); } Console . ReadKey (); } } } 

Use the Console.ReadKey () command; to see the results more clearly.

Compiling and running the above C # program will produce the following results:

String (String) in C # Picture 3

Get substring in C #:

 using System ; namespace QTMCsharp { class TestCsharp { static void Main ( string [] args ) { Console . WriteLine ( "Lay chuoi con trong C#" ); Console . WriteLine ( "-------------------------------------" ); string str = "Lay chuoi con trong C#" ; Console . WriteLine ( "Chuoi ban dau: " + str ); string substr = str . Substring ( 10 ); Console . WriteLine ( "Chuoi con: " + substr ); Console . ReadKey (); } } } 

Use the Console.ReadKey command () ; to see the results more clearly.

Compiling and running the above C # program will produce the following results:

String (String) in C # Picture 4

String concatenation in C #:

 using System ; namespace QTMCsharp { class TestCsharp { static void Main ( string [] args ) { Console . WriteLine ( "Noi chuoi trong C#" ); Console . WriteLine ( "-------------------------------------" ); string [] starray = new string []{ "Hoc C# co ban va nang cao." , "Chuong nay trinh bay ve chuoi trong C#." , "Chung ta dang tim hieu ve noi chuoi trong C#." , "chuc cac ban hoc tot." , "Xin tran trong cam on!" }; string str = String . Join ( "n" , starray ); Console . WriteLine ( str ); Console . ReadKey (); } } } 

If you do not use the Console.ReadKey () command; then the program will run and finish (so fast that you can not see the results). This command allows us to see the results more clearly.

Compiling and running the above C # program will produce the following results:

String (String) in C # Picture 5

Follow tutorialspoint

Previous article: Array (Array) in C #

Next article: Structure (Struct) in C #

5 ★ | 1 Vote | 👨 106 Views
« PREV POST
NEXT POST »