C# provides a variety of basic string operations to manipulate and work with strings. Here are some of the most commonly used string operations in C#:
Concatenate:
- Two strings can be concatenated either by using a System.String.Concat or by using + operator.
string str1 = "This is one";
string str2 = "This is two";
string str2 = str1 + str2;
//Output: This is one This is two
Replace:
- Replace(a,b) is used to replace a string with another string.
string str1 = "This is one";
string str2 = str1.Replace(“one”, “two”);
//Output: This is two
Trim:
- Trim() is used to trim the white spaces in a string at the end.
string str1 = "This is one ";
str1.Trim();
//Output: "This is one“
Contains:
- Check if a string contains a pattern of substring or not.
string str = "This is test";
bool result = str.Contains("test");
//Output: true
These are just a few of the basic string operations in C#. The String class provides many more methods and properties for working with strings, such as searching, replacing, trimming, and more.