How to Get First Character of Each Word in String C#.net?

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”.

get first character of each word in string c#.net
get first character of each word in string c#.net

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: