C# String Functions and Properties

logotype In this chapter you will learn
  • What is string?
  • How many types of string function in C#?
  • How to use C# string function in programming?
  • What is the difference between string and String?

In C# programming, string is another kind of data type that represents Unicode Characters. It is the alias of System.String, however, you can also write System.String instead of a string. It is the sequence of character in which each character is a Unicode character.

There is no difference between string and String because a string is the alias of System.String. Most of the developers get confused what to use between sting and String. Technically there is no difference between them and they can use any of them. However, you will have to use “using System” to use the String in C#. Another difference is String is a class name whereas a string is a reserved keyword. You should always use string instead of String.

C# string function

String Functions Definitions
Clone() Make clone of string.
CompareTo() Compare two strings and returns integer value as output. It returns 0 for true and 1 for false.
Contains() The C# Contains method checks whether specified character or string is exists or not in the string value.
EndsWith() This EndsWith Method checks whether specified character is the last character of string or not.
Equals() The Equals Method in C# compares two string and returns Boolean value as output.
GetHashCode() This method returns HashValue of specified string.
GetType() It returns the System.Type of current instance.
GetTypeCode() It returns the Stystem.TypeCode for class System.String.
IndexOf() Returns the index position of first occurrence of specified character.
ToLower() Converts String into lower case based on rules of the current culture.
ToUpper() Converts String into Upper case based on rules of the current culture.
Insert() Insert the string or character in the string at the specified position.
IsNormalized() This method checks whether this string is in Unicode normalization form C.
LastIndexOf() Returns the index position of last occurrence of specified character.
Length It is a string property that returns length of string.
Remove() This method deletes all the characters from beginning to specified index position.
Replace() This method replaces the character.
Split() This method splits the string based on specified value.
StartsWith() It checks whether the first character of string is same as specified character.
Substring() This method returns substring.
ToCharArray() Converts string into char array.
Trim() It removes extra whitespaces from beginning and ending of string.

Programming Examples of C# String Function:

  1. using System.Text;
  2. namespace string_function
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. string firstname;
  9. string lastname;
  10. firstname = "Steven Clark";
  11. lastname = "Clark";
  12. Console.WriteLine(firstname.Clone());
  13. // Make String Clone
  14. Console.WriteLine(firstname.CompareTo(lastname));
  15. //Compare two string value and returns 0 for true and
  16. 1 for false
  17.  
  18. Console.WriteLine(firstname.Contains("ven")); //Check whether specified value exists or not in string
  19.  
  20. Console.WriteLine(firstname.EndsWith("n")); //Check whether specified value is the last character of string
  21. Console.WriteLine(firstname.Equals(lastname));
  22. //Compare two string and returns true and false
  23.  
  24.  
  25. Console.WriteLine(firstname.GetHashCode());
  26. //Returns HashCode of String
  27.  
  28. Console.WriteLine(firstname.GetType());
  29. //Returns type of string
  30.  
  31. Console.WriteLine(firstname.GetTypeCode());
  32. //Returns type of string
  33.  
  34. Console.WriteLine(firstname.IndexOf("e")); //Returns the first index position of specified value
  35. the first index position of specified value
  36.  
  37. Console.WriteLine(firstname.ToLower());
  38. //Covert string into lower case
  39.  
  40. Console.WriteLine(firstname.ToUpper());
  41. //Convert string into Upper case
  42.  
  43. Console.WriteLine(firstname.Insert(0, "Hello")); //Insert substring into string
  44.  
  45. Console.WriteLine(firstname.IsNormalized());
  46. //Check Whether string is in Unicode normalization
  47. from C
  48.  
  49.  
  50. Console.WriteLine(firstname.LastIndexOf("e")); //Returns the last index position of specified value
  51.  
  52. Console.WriteLine(firstname.Length);
  53. //Returns the Length of String
  54.  
  55. Console.WriteLine(firstname.Remove(5));
  56. //Deletes all the characters from begining to specified index.
  57.  
  58. Console.WriteLine(firstname.Replace('e','i')); // Replace the character
  59. string[] split = firstname.Split(new char[] { 'e' }); //Split the string based on specified value
  60.  
  61.  
  62. Console.WriteLine(split[0]);
  63. Console.WriteLine(split[1]);
  64. Console.WriteLine(split[2]);
  65. Console.WriteLine(firstname.StartsWith("S")); //Check wheter first character of string is same as specified value
  66.  
  67. Console.WriteLine(firstname.Substring(2,5));
  68. //Returns substring
  69.  
  70. Console.WriteLine(firstname.ToCharArray());
  71. //Converts an string into char array.
  72.  
  73. Console.WriteLine(firstname.Trim());
  74. //It removes starting and ending white spaces from
  75. string.
  76. }
  77. }
  78. }

Output

Steven Clark
1
True
False
False
1470518261
System.String
String
2
steven clark
STEVEN CLARK
HelloSteven Clark
True
4
12
Steve
Stivin Clark
St
v
n Clark
True
even
Steven Clark
Steven Clark
_
 

Share your thought