String manipulation in C#

Mohammad Zubair 0
C# Tips Thumbnail

In C#, there are many built-in methods and functions that can be used to manipulate strings. Here are some examples:

Concatenation: To join two or more strings together, you can use the “+” operator or the String.Concat() method. For example:

string firstName = "John";
string lastName = "Doe";
string fullName = firstName + " " + lastName; // Output: "John Doe"
string fullName2 = String.Concat(firstName, " ", lastName); // Output: "John Doe"

Length: To get the length of a string, you can use the Length property. For example:

string str = "Hello, World!";
int length = str.Length; // Output: 13

Substring: To get a substring from a string, you can use the Substring() method. For example:

string str = "Hello, World!";
string substring = str.Substring(7); // Output: "World!"

Replace: To replace a substring in a string, you can use the Replace() method. For example:

string str = "Hello, World!";
string newStr = str.Replace("World", "Universe"); // Output: "Hello, Universe!"

 


Mohammad Zubair

I'm Mohammad Zubair, a passionate software engineer working in the dynamic world of IT. Currently, I'm proud to be a part of HawarIT, a thriving Dutch-Bangladeshi joint venture company, where I contribute my expertise and enthusiasm to the field of software engineering.

Leave a Reply

Your email address will not be published. Required fields are marked *