A string is a collection of characters. In C#, String is a reference data type, the benefit of reference data type is that it can reduce or increase the size of string at runtime.
S.No. | Function | Syntax | Description |
---|---|---|---|
Length | int len = str.Length; | Calculate the number of characters and return an integer value. | |
ToLower() | string x = a.ToLower(); | Use to convert a String into Lowercase | |
ToUpper() | string x = a.ToUpper(); | Use to convert a String into Uppercase | |
Copy() | string c = string.Copy(a); string c = a; | Use to copy a string. It is similar to assigning a string to another string. | |
Concat() | string c = string.Concat(a, b); | Use to concatenate strings, you can also use '+' operator to concatenate. | |
StartsWith() | bool c = a.StartsWith("New"); | Find a string is starting with a specified string or not, returns a boolean value. | |
EndsWith() | bool c = a.StartsWith("i"); | Same as StartsWith(), instead it search a string from end. | |
IndexOf() | int c = a.IndexOf('a'); | Search a character or a string and return its index position. | |
Replace() | string c = a.Replace(",", "!!"); | Replace characters from the string with a new character or string. | |
Insert() | string c = a.Insert(5, "!"); | Use to insert a character or a string at a specified index location. | |
Remove() | string c = a.Remove(5); string c = a.Remove(5,2); | Remove characters from the specified location till end of the string. However, you can also specify the number of characters to remove. In Syntax 1: delete from 5th character to end of the list. Syntax 2: delete only 2 characters starting from 5th position. | |
Substring() | string s = a.Substring(5); //5th index to end string s2 = a.Substring(5,3); //5th to 8th index. | Use to retrieve a substring from a string starting from a specified character position. You can also specify the length. | |
Contains() | bool b = s.Contains("a"); | Returns a boolean value indicating whether the specified String occurs within a string. | |
ToCharArray() | char[] arr = str.ToCharArray(); | Use to convert a String into Array of characters. | |
StrReverse() | char[] c = s.ToCharArray(); Array.Reverse(c); | Reverse array of characters | |
Equals() | s1.Equals(s2) | Check two strings for their equality and returns a boolean value. Returns true, if both the strings are equal, otherwise return false. | |
Compare() or CompareTo | int x = string.Compare(a, b); | Compare two strings and return an integer value. Return zero if both the strings are equal, Return greater than zero if first string is greater, and Return less than zero if second string is greater. | |
Split() | string[] s = a.Split(' '); | Use to split a string by specifying delimiter character and return array of string. | |
Trim() | string c = a.Trim(' '); string c = a.Trim('#'); | Use to remove leading and trailing spaces, and also to remove any unwanted characters. Syntax 2 will remove leading and trailing # characters. | |
PadLeft() | string c = a.PadLeft(20,'*'); | Use to fill characters in the beginning of the output, if the length of a string is smaller than the total width. | |
PadRight() | string c = a.PadLeft(20,'*'); | Same as PadLeft instead it fills a character on the right of a string. | |
Tryparse() | int i; bool c = Int32.TryParse(a, out i); | Use to check if the string contains integer value or not, returns a boolean value. Tryparse checks for a valid range. |
Example 1: Input string and find its length.
class Program { static void Main(string[] args) { int len; String name; Console.WriteLine("Enter name"); name = Console.ReadLine(); len = name.Length; Console.WriteLine("Length = " + len); Console.ReadLine(); } }
Example 2: Input string and convert into lowercase and uppercase
static void Main(string[] args) { string s; Console.WriteLine("Enter string: "); s = Console.ReadLine(); Console.WriteLine("String in lowercase is " + s.ToLower()); Console.WriteLine("String in uppercase is " + s.ToUpper()); Console.ReadLine(); }
Example 3: Concatenate 2 strings
//input 2 numbers in string and sum them static void Main(string[] args) { string s1, s2, s3; Console.WriteLine("Enter 2 strings"); s1 = Console.ReadLine(); s2 = Console.ReadLine(); s3 = s1 + s2; //concatenate (merge, join) Console.WriteLine("Sum = " + s3); Console.ReadLine(); }
Example 4: Check string is starting or ending with a particular string or not.
static void Main(string[] args) { string a = "New Delhi"; string b = "New York"; string c = "Old City"; Console.WriteLine(a.StartsWith("new")); //true Console.WriteLine(b.StartsWith("new")); //true Console.WriteLine(c.StartsWith("new")); //false Console.WriteLine(a.EndsWith("Delhi")); //true Console.WriteLine(b.EndsWith("Delhi")); //false Console.WriteLine(c.EndsWith("Delhi")); //false Console.ReadLine(); }
Example 5: Find character position using IndexOf
static void Main(string[] args) { string s; int i; Console.WriteLine("Enter string: "); s = Console.ReadLine(); i = s.IndexOf('a'); //i = s.IndexOf("hi"); //i = s.IndexOf('e',3); //i = s.IndexOf("prg", 2); Console.WriteLine("Index of a is " + i); Console.ReadLine(); }
Example 6: Replace a character or string.
static void Main(string[] args) { string s; Console.WriteLine("Enter string: "); s = Console.ReadLine(); s = s.Replace(" ", ","); Console.WriteLine("After replacing new string is: " + s); Console.ReadLine(); }
Example 7: Insert a character or string in between a string
static void Main(string[] args) { string s; Console.WriteLine("Enter string: "); s = Console.ReadLine(); s = s.Insert(5, "!"); Console.WriteLine("After inserting new string is: " + s); Console.ReadLine(); }
Example 8: Remove a character or string in between the string.
static void Main(string[] args) { string s; Console.WriteLine("Enter string: "); s = Console.ReadLine(); s = s.Remove(2); //s = s.Remove(2,1); Console.WriteLine("After removing string is: " + s); Console.ReadLine(); }
Example 9: Input string and return substring.
static void Main(string[] args) { String s1, s2; Console.WriteLine("Enter name"); s1 = Console.ReadLine(); s2 = s1.Substring(2); // start from length 2 //s2 = s1.Substring(2,3); //start length 2, no of char 3 Console.WriteLine("New String = " + s2); Console.ReadLine(); }
Example 10: Find vowel from input string using ToCharArray() method.
static void Main(string[] args) { string s; int count=0; Console.WriteLine("Enter string: "); s = Console.ReadLine(); char []ch = s.ToCharArray(); foreach(char c in ch) { if(c=='a' || c=='e' || c=='i' || c='o' || c=='u') { count++; } } Console.WriteLine("Total vowels are : " + count); Console.ReadLine(); }
Example 11: Compare 2 string and find which is greater.
static void Main(string[] args) { string a = "samesh"; string b = "ramesh nagar"; //int i = a.CompareTo(b); int i = string.Compare(a, b); //same as above statment //Console.WriteLine("Value = " + i); /*Above statement will print value: * print 0 if both r equal * print -1 if 2nd str is greater * print 1 if 1st str is greater. */ if(i==0) { Console.WriteLine("Both are equal"); } else if(i<0) { Console.WriteLine("2nd str is greater"); } else { Console.WriteLine("1st str is greater"); } Console.ReadLine(); }
Example 12: Trim leading and trailing characters
static void Main(string[] args) { string s; Console.WriteLine("Enter string: "); s = Console.ReadLine(); Console.WriteLine("Before trim string is:" + s); s = s.Trim(' '); //s = s.Trim('#'); //s = s.Trim('a'); Console.WriteLine("After trim string is:" + s); Console.ReadLine(); }
Example 13: PadLeft & PadRight functions
static void Main(string[] args) { string s; Console.WriteLine("Enter string: "); s = Console.ReadLine(); Console.WriteLine("Before PadLeft string is:" + s); s = s.PadLeft(20, '*'); //s = s.PadRight(20, '*'); Console.WriteLine("After PadLeft string is:" + s); Console.ReadLine(); }
Example 14: tryparse functions
static void Main(string[] args) { string a = "Moti nagar"; string b = "Ramesh nagar"; string creditcard = "7250"; int i; Console.WriteLine(Int32.TryParse(a, out i)); Console.WriteLine(Int32.TryParse(b, out i)); Console.WriteLine(Int32.TryParse(creditcard, out i)); if(Int32.TryParse(a, out i)) { Console.WriteLine(Convert.ToInt32(a)*2); } if (Int32.TryParse(b, out i)) { Console.WriteLine(Convert.ToInt32(b) * 2); } if (Int32.TryParse(creditcard, out i)) { Console.WriteLine(Convert.ToInt32(creditcard) * 2); } Console.ReadLine(); }