c-sharp
  1. c-sharp-program-to-remove-all-characters-in-a-string-except-alphabets

Program to Remove all Characters in a String Except Alphabets - (C# Basic Programs)

In this tutorial, we will discuss a program to remove all characters in a string except alphabets using the C# programming language.

Syntax

The syntax for removing all characters in a string except alphabets is as follows:

using System;

class Program {
    static void Main(string[] args) {
        string inputString = "Sample123 String4$^";
        string outputString = "";
        for (int i = 0; i < inputString.Length; i++) {
            if (Char.IsLetter(inputString[i]))
                outputString += inputString[i];
        }
        Console.WriteLine(outputString);
    }
}

Example

using System;

class Program {
    static void Main(string[] args) {
        string inputString = "Sample123 String4$^";
        string outputString = "";
        for (int i = 0; i < inputString.Length; i++) {
            if (Char.IsLetter(inputString[i]))
                outputString += inputString[i];
        }
        Console.WriteLine(outputString);
    }
}

Output

SampleString

Explanation

This program removes all characters in a string except alphabets by iterating over each character in the input string and checking if it is a letter using the Char.IsLetter() function. If the current character is a letter, it is appended to the output string, otherwise, it is skipped.

Use

This program is useful when you need to extract only alphabets from a string and remove all other characters. It can be used in scenarios where you need to process a string-like user input for searching or indexing purposes.

Summary

In this tutorial, we have discussed a program to remove all characters in a string except alphabets using the C# programming language. We have seen the syntax, example, explanation, use, and output of removing non-alphabetic characters from a string. By practicing and understanding this program, C# developers can develop a better understanding of string manipulation and improve their coding abilities.

Published on: