HARISH KANCHAN
Methods of C# string
There are various string methods in C#. Some of them are as follows:
Remove() In C#, Remove() is used for removing the characters from the specified
position of a string. If the length is not specified, then it will remove all the characters
after specified position.
Sample Example
// C# program to illustrate the
// public string Remove(int StartIndex)
using System;
class Geeks {
// Main Method
public static void Main()
{
// define string
String str = "HARISHKANCHAN";
Console.WriteLine("Given String : " + str);
// delete from index 4 to end of string
Console.WriteLine("New String1 : " + str.Remove(4));
// delete character from index 8 to end of string
Console.WriteLine("New String2 : " + str.Remove(8));
}
}
• Output:
• Given String : HARISHKANCHAN
• New String1 : HARI
• New String2 : HARISHKA
Equals() In C#.NET, the Equals() method is a part of the System.String class, and it
is used to compare the contents of two strings to determine if they are equal. The
method returns a Boolean value indicating whether the two strings are equal or not.
Dept of Computer Science, Dr.BB Hegde First Grade College Kundapur
HARISH KANCHAN
Replace() The Replace() method in C#.NET is a built-in method of the System.String
class that is used to replace all occurrences of a specified substring or characters with
another substring. The method returns a new string with the replacements made.
For example
string originalString = "Hello, Kanchan";
string replacedString = originalString.Replace("Kanchan", "Friend");
Console.WriteLine(replacedString);
Output: Hello Friend
Split() The Split() method in C#.NET is a built-in method of the System.String class
that is used to split a string into an array of substrings based on a specified delimiter.
The result is an array of strings containing the substrings.
Example:
string data = "apple,orange;banana";
char[] separators = { ',', ';' };
string[] fruits = data.Split(separators);
foreach (string fruit in fruits)
Console.WriteLine(fruit);
Console.ReadKey();
Output:
apple
orange
banana
Dept of Computer Science, Dr.BB Hegde First Grade College Kundapur
HARISH KANCHAN
Methods Description
Format() returns a formatted string
Split() splits the string into substring
Substring() returns substring of a string
Compare() compares string objects
Replace() replaces the specified old character with the specified new character
Contains() checks whether the string contains a substring
Join() joins the given strings using the specified separator
Trim() removes any leading and trailing whitespaces
EndsWith() checks if the string ends with the given string
IndexOf() returns the position of the specified character in the string
ToUpper() converts the string to uppercase
ToLower() converts the string to lowercase
returns string padded with spaces or with a specified Unicode
PadLeft()
character on the left
returns string padded with spaces or with a specified Unicode
PadRight()
character on the right
StartsWith() checks if the string begins with the given string
ToCharArray() converts the string to a char array
LastIndexOf() returns index of the last occurrence of a specified string
Dept of Computer Science, Dr.BB Hegde First Grade College Kundapur