In this C# tutorial, we will go through how to get the first character of each word in a string c#.net.
Get First Character of Each Word in String C#.net using Split method
A string is a sequence of characters. In C#, you can manipulate strings in a variety of ways. One common operation is to split a string into words, which can be achieved using the Split() method. In our case, we’ll be splitting a string into words and then getting the first character from each word in C#.Net.
In C#.NET, to get the first character of each word in a string, we use a combination of the Split
() method and string indexing.
The Split
method is used to divide the string into an array of words based on a specified delimiter (in this case, a space character). After splitting the string, we iterate over each word in the array.
String indexing (word[0]
) is then used to access the first character of each word. We collect these first characters and concatenate them into a single string to get our final result.
The process can be done within a method, which takes the input string as a parameter and returns the string of first characters. For efficiency in concatenation, we use the StringBuilder
class.
This approach works well when words are separated by spaces and there are no leading, trailing, or consecutive spaces.
Let us now see, how to create a c#.net console application to test it.
Step 1: Create a new C# Project
First, we need to create a new C# project in Visual Studio.
- Open Visual Studio.
- Go to File -> New -> Project.
- Select “Console App (.NET Core)”.
- Name your project and click “Create”.
Step 2: Write a function to get the first character of each word
Next, we’ll write a function named GetFirstCharacterOfEachWord
. This function will split a string into words, and then return a string with the first character of each word. Here’s how to do it:
public static string GetFirstCharacterOfEachWord(string str)
{
// Split the string into words
string[] words = str.Split(' ');
// Use StringBuilder for efficiency when concatenating strings in a loop
StringBuilder firstCharacters = new StringBuilder();
// Iterate over each word
foreach (string word in words)
{
if (!String.IsNullOrEmpty(word))
{
// Get the first character of the word and append it to the StringBuilder
firstCharacters.Append(word[0]);
}
}
// Return the resulting string
return firstCharacters.ToString();
}
This function works by splitting the input string into an array of words using the Split(' ')
method, which splits the string at each space character. It then creates a new StringBuilder
object to hold the first characters of each word. It then iterates over each word in the array.
If the word is not empty (which could happen if there are multiple space characters in a row), it gets the first character of the word using word[0]
and appends it to the StringBuilder
.
Finally, it returns the resulting string using firstCharacters.ToString()
.
Step 3: Use the function in Main() method
Now that we have our function, we can use it in the Main
method to test it.
static void Main(string[] args)
{
string cities = "New York Los Angeles Chicago Houston Phoenix Philadelphia San Antonio San Diego Dallas";
string firstCharacters = GetFirstCharacterOfEachWord(cities);
Console.WriteLine(firstCharacters);
}
In this example, we have a string of city names from the USA. When we run the program, it will print the first character of each city name.
Step 4: Run the program
Now, you can run the program by pressing F5 or clicking on the “Start Debugging” button. You should see the output in the console window.
If your input string was “New York Los Angeles Chicago Houston Phoenix Philadelphia San Antonio San Diego Dallas”, the output should be “NYLACHPPSASDD”.
In this tutorial, we learned how to get the first character of each word in a string c#.net. We saw a complete program to get the first character of each word in a string c#.net.
You may like the following tutorials:
- How to create a console application in visual studio code?
- How to get the first n characters of a string in c#.net
- Convert Int to Binary String with Leading Zeros in C#.Net
- Assign Null Value to a String in C#.NET
- How to handle int null values 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…