A collection of characters are called string. The String variable is used to store the sequence of characters enclosed in double-quotes.

string function c#, string c#, string and its functions in c#, c# substring, c# string format, c# string concat, csharp


Since a string is an alias for System.String, there is no distinction between string and String. The majority of developers are unsure which to use between sting and String. They can use both of them and there is no technical distinction between them. The little distinction between string and String is that a string is a reserved keyword, while String is a class name. Instead of String, you should use string.

System.String has a variety of methods that you'd expect from a utility class like this, such as getting the length of the character data, finding substrings inside the current string, and converting to and from uppercase and lowercase. Consider the following table:

Members

Definition

Length

This property returns the current string's length.

Compare()

Two strings are compared and their sort order is defined using this static method.

If the return value is greater than zero than the first string follows the second string in sort order and vice versa if value is less than zero.

Contains()

This method checks for the presence of a certain substring in a string.

Equals()

This method compares two string objects to see if they have the same character data.

Format()

This static approach formats a string with the help of other primitives (for example, numerical data and other strings).

Insert()

This method is used to add a string within another string.

PadLeft()

These methods are used to add certain characters to a string.

PadRight()

Remove()

These method is used to get a modified version of a string (characters omitted or replaced).

Replace()

Split()

This method returns a String array with the substrings delimited by elements of a given char array or string array in this case.

Trim()

This method excludes all instances of a given set of characters from the current string's beginning and end.

ToUpper()

These methods generate an uppercase or lowercase copy of the current series, respectively.

ToLower()

Concat()

This method add up two strings and make one.

 

Consider the following example to have the better understanding of working of these functions

string first = "Banana";

string second = "Apple";

string fruits = "Banana, Apple, Kiwi, Oranges";

 

Console.WriteLine($"Length of first is: {first.Length}");

Console.WriteLine($"Output of Compare() method: {string.Compare(first,second)}");

Console.WriteLine($"Output of Contains() method: {first.Contains("an")}");

Console.WriteLine($"Output of Equals() method: {first.Equals("Banana")}");

Console.WriteLine($"Output of Format() method: {string.Format(second,"an")}");

Console.WriteLine($"Output of Insert() method: {first.Insert(3,second)}");

Console.WriteLine($"Output of PadLeft() method: {first.PadLeft(12,'a')}");

Console.WriteLine($"Output of PadRight() method: {first.PadRight(9, 'c')}");

Console.WriteLine($"Output of Remove() method: {first.Remove(1,3)}");

Console.WriteLine($"Output of Replace() method: {first.Replace('a','e')}");

Console.WriteLine($"Output of Trim() method: {first.Trim('n')}");

Console.WriteLine($"Output of ToUpper() method: {first.ToUpper()}");

Console.WriteLine($"Output of ToLower() method: {first.ToLower()}");

Console.WriteLine($"Output of Concat() method: {string.Concat(first," Shake")}");

string[] fruitList = fruits.Split(',');

Console.WriteLine("Output of Split() method:");

foreach (var item in fruitList)

{

  Console.WriteLine(item);

}

Output:

Length of first is: 6

Output of Compare() method: 1

Output of Contains() method: True

Output of Equals() method: True

Output of Format() method: Apple

Output of Insert() method: BanAppleana

Output of PadLeft() method: aaaaaaBanana

Output of PadRight() method: Bananaccc

Output of Remove() method: Bna

Output of Replace() method: Benene

Output of Trim() method: Banana

Output of ToUpper() method: BANANA

Output of ToLower() method: banana

Output of Concat() method: Banana Shake

Output of Split() method:

Banana

 Apple

 Kiwi

 Oranges


That’s all about string function in c#. If you found this article helpful share it with others. Let me know your queries and suggestions in the comment section below. Do subscribe my site and channel as I don’t want you to miss any update.

Moreover, you can also contact me and share your queries on Instagram, Facebook or Twitter. I would love to provide a solution to your issues.

Stay tuned and strive to learn. Keep learning, keep growing!