In this C#.Net tutorial, we will be exploring how to remove the last word from a string in C#.NET. Also, we will see, how to add a word to the end of a string in C#.Net.
Remove the last word from string C#.Net
For this example, I have used visual studio 2022 and C#.Net as language.
Steps to Remove the Last Word from a String
Step 1: Define the String
First, you need to define the string from which you want to remove the last word. In this case, let’s use the name of a city in the USA.
string cityName = "San Francisco California";
Step 2: Find the Index of the Last Space
The next step is to find the position of the last space in the string. We assume that words are separated by spaces. In C#, you can use the LastIndexOf()
method to achieve this.
int lastSpaceIndex = cityName.LastIndexOf(' ');
This will return the index of the last space in the string. If no space is found, it will return -1.
Step 3: Remove the Last Word
Now that you have the index of the last space, you can remove the last word from the string. You can use the Substring()
method in C# to extract a part of the string.
if (lastSpaceIndex != -1) // if a space was found
{
cityName = cityName.Substring(0, lastSpaceIndex);
}
This will remove the last word from the string.
Full Code Example
Here’s the full code example:
string cityName = "San Francisco California";
int lastSpaceIndex = cityName.LastIndexOf(' ');
if (lastSpaceIndex != -1) // if a space was found
{
cityName = cityName.Substring(0, lastSpaceIndex);
}
Console.WriteLine(cityName);
When you run this code, it will output: San Francisco
.
You can see the image below:
Now, you should now be able to remove the last word from any string in C#.NET. Remember, this assumes that words are separated by spaces. If you have a different word separator, you would need to adjust the code accordingly.
Add a Word to the End of a String in C#.NET
Now, let us see, how to add a word to the end of a string in C#.net.
You should have a development environment for C# and some basic knowledge of C# syntax and string manipulation.
Step 1: Define the String
First, you need to define the string to which you want to add a word. As before, we’ll use a city name from the USA as an example.
string cityName = "San Francisco";
Step 2: Define the Word to Add
Next, define the word you want to add to the end of the string.
string stateName = "California";
Step 3: Add the Word to the String
Now you can add the word to the string. In C#, you can use the +
operator or the Concat()
method to achieve this.
// Using the + operator
cityName = cityName + " " + stateName;
// Or using the Concat() method
cityName = string.Concat(cityName, " ", stateName);
Both of these methods will append the state name to the city name with a space in between.
Full Code Example
Here’s the full code example that I have run in a C#.net console application.
string cityName = "San Francisco";
string stateName = "California";
// Using the + operator
cityName = cityName + " " + stateName;
// Or using the Concat() method
cityName = string.Concat(cityName, " ", stateName);
Console.WriteLine(cityName);
Here you can see the output below:
Conclusion
In this C#.Net tutorial, we have discussed how to remove the last word from a string c#.net and also I have explained how to add a word to the end of a string in C#.net.
You may also like the following C#.Net tutorials:
- Convert String to Int Without Parse in C#.Net
- How to remove first and last character from string using C#.net?
- Convert String to DateTime in C#.NET
Bijay Kumar is a renowned software engineer, accomplished author, and distinguished Microsoft Most Valuable Professional (MVP) specializing in SharePoint. With a rich professional background spanning over 15 years, Bijay has established himself as an authority in the field of information technology. He possesses unparalleled expertise in multiple programming languages and technologies such as ASP.NET, ASP.NET MVC, C#.NET, and SharePoint, which has enabled him to develop innovative and cutting-edge solutions for clients across the globe. Read more…