Posted on by Kalkicode
Code String

Remove all characters in a string except alphabet

Here given code implementation process.

/*
    Java Program for
    Remove all characters in a string except alphabet
*/
public class RemoveCharacters
{
	// Remove all characters other than alphabets from string
	public void removeNonAlphabets(String text)
	{
		// Get the length
		int n = text.length();
		if (n == 0)
		{
			return;
		}
		String result = "";
		int i = 0;
		while (i < n)
		{
			if (text.charAt(i) >= 'a' && text.charAt(i) <= 'z' || 
                (text.charAt(i) >= 'A' && text.charAt(i) <= 'Z'))
			{
				// When alphabets character
				result = result + text.charAt(i);
			}
			i++;
		}
		System.out.println(" Given text : " + text);
		System.out.println(" Result     : " + result);
	}
	public static void main(String[] args)
	{
		RemoveCharacters task = new RemoveCharacters();
		// Test
		task.removeNonAlphabets("!*&3M'?an/<go_,j#u$$ic^e(&)");
		task.removeNonAlphabets("5@23A23B.C");
	}
}

Output

 Given text : !*&3M'?an/<go_,j#u$$ic^e(&)
 Result     : Mangojuice
 Given text : 5@23A23B.C
 Result     : ABC
// Include header file
#include <iostream>
#include <string>

using namespace std;
/*
    C++ Program for
    Remove all characters in a string except alphabet
*/
class RemoveCharacters
{
	public:
		// Remove all characters other than alphabets from string
		void removeNonAlphabets(string text)
		{
			// Get the length
			int n = text.length();
			if (n == 0)
			{
				return;
			}
			string result = "";
			int i = 0;
			while (i < n)
			{
				if (text[i] >= 'a' && text[i] <= 'z' || 
                    (text[i] >= 'A' && text[i] <= 'Z'))
				{
					// When alphabets character
					result = result  +  text[i];
				}
				i++;
			}
			cout << " Given text : " << text << endl;
			cout << " Result     : " << result << endl;
		}
};
int main()
{
	RemoveCharacters *task = new RemoveCharacters();
	// Test
	task->removeNonAlphabets("!*&3M\'?an/<go_,j#u$$ic^e(&)");
	task->removeNonAlphabets("5@23A23B.C");
	return 0;
}

Output

 Given text : !*&3M'?an/<go_,j#u$$ic^e(&)
 Result     : Mangojuice
 Given text : 5@23A23B.C
 Result     : ABC
package main
import "fmt"
/*
    Go Program for
    Remove all characters in a string except alphabet
*/
type RemoveCharacters struct {}
func getRemoveCharacters() * RemoveCharacters {
	var me *RemoveCharacters = &RemoveCharacters {}
	return me
}
// Remove all characters other than alphabets from string
func(this RemoveCharacters) removeNonAlphabets(text string) {
	// Get the length
	var n int = len(text)
	if n == 0 {
		return
	}
	var result string = ""
	var i int = 0
	for (i < n) {
		if text[i] >= 'a' && text[i] <= 'z' || 
		(text[i] >= 'A' && text[i] <= 'Z') {
			// When alphabets character
			result = result + string(text[i])
		}
		i++
	}
	fmt.Println(" Given text : ", text)
	fmt.Println(" Result     : ", result)
}
func main() {
	var task * RemoveCharacters = getRemoveCharacters()
	// Test
	task.removeNonAlphabets("!*&3M'?an/<go_,j#u$$ic^e(&)")
	task.removeNonAlphabets("5@23A23B.C")
}

Output

 Given text :  !*&3M'?an/<go_,j#u$$ic^e(&)
 Result     :  Mangojuice
 Given text :  5@23A23B.C
 Result     :  ABC
// Include namespace system
using System;
/*
    Csharp Program for
    Remove all characters in a string except alphabet
*/
public class RemoveCharacters
{
	// Remove all characters other than alphabets from string
	public void removeNonAlphabets(String text)
	{
		// Get the length
		int n = text.Length;
		if (n == 0)
		{
			return;
		}
		String result = "";
		int i = 0;
		while (i < n)
		{
			if (text[i] >= 'a' && text[i] <= 'z' || 
                (text[i] >= 'A' && text[i] <= 'Z'))
			{
				// When alphabets character
				result = result + text[i];
			}
			i++;
		}
		Console.WriteLine(" Given text : " + text);
		Console.WriteLine(" Result     : " + result);
	}
	public static void Main(String[] args)
	{
		RemoveCharacters task = new RemoveCharacters();
		// Test
		task.removeNonAlphabets("!*&3M\'?an/<go_,j#u$$ic^e(&)");
		task.removeNonAlphabets("5@23A23B.C");
	}
}

Output

 Given text : !*&3M'?an/<go_,j#u$$ic^e(&)
 Result     : Mangojuice
 Given text : 5@23A23B.C
 Result     : ABC
<?php
/*
    Php Program for
    Remove all characters in a string except alphabet
*/
class RemoveCharacters
{
	// Remove all characters other than alphabets from string
	public	function removeNonAlphabets($text)
	{
		// Get the length
		$n = strlen($text);
		if ($n == 0)
		{
			return;
		}
		$result = "";
		$i = 0;
		while ($i < $n)
		{
			if ($text[$i] >= 'a' && $text[$i] <= 'z' || 
                ($text[$i] >= 'A' && $text[$i] <= 'Z'))
			{
				// When alphabets character
				$result = $result.strval($text[$i]);
			}
			$i++;
		}
		echo(" Given text : ".$text.
			"\n");
		echo(" Result     : ".$result.
			"\n");
	}
}

function main()
{
	$task = new RemoveCharacters();
	// Test
	$task->removeNonAlphabets("!*&3M\'?an/<go_,j#u$\$ic^e(&)");
	$task->removeNonAlphabets("5@23A23B.C");
}
main();

Output

 Given text : !*&3M\'?an/<go_,j#u$$ic^e(&)
 Result     : Mangojuice
 Given text : 5@23A23B.C
 Result     : ABC
/*
    Node JS Program for
    Remove all characters in a string except alphabet
*/
class RemoveCharacters
{
	// Remove all characters other than alphabets from string
	removeNonAlphabets(text)
	{
		// Get the length
		var n = text.length;
		if (n == 0)
		{
			return;
		}
		var result = "";
		var i = 0;
		while (i < n)
		{
			if (text.charAt(i) >= 'a' && text.charAt(i) <= 'z' || 
                (text.charAt(i) >= 'A' && text.charAt(i) <= 'Z'))
			{
				// When alphabets character
				result = result + text.charAt(i);
			}
			i++;
		}
		console.log(" Given text : " + text);
		console.log(" Result     : " + result);
	}
}

function main()
{
	var task = new RemoveCharacters();
	// Test
	task.removeNonAlphabets("!*&3M\'?an/<go_,j#u$$ic^e(&)");
	task.removeNonAlphabets("5@23A23B.C");
}
main();

Output

 Given text : !*&3M'?an/<go_,j#u$$ic^e(&)
 Result     : Mangojuice
 Given text : 5@23A23B.C
 Result     : ABC
#    Python 3 Program for
#    Remove all characters in a string except alphabet
class RemoveCharacters :
	#  Remove all characters other than alphabets from string
	def removeNonAlphabets(self, text) :
		#  Get the length
		n = len(text)
		if (n == 0) :
			return
		
		result = ""
		i = 0
		while (i < n) :
			if (text[i] >= 'a'
				and text[i] <= 'z'
				or(text[i] >= 'A'
					and text[i] <= 'Z')) :
				#  When alphabets character
				result = result + str(text[i])
			
			i += 1
		
		print(" Given text : ", text)
		print(" Result     : ", result)
	

def main() :
	task = RemoveCharacters()
	#  Test
	task.removeNonAlphabets("!*&3M\'?an/<go_,j#u$$ic^e(&)")
	task.removeNonAlphabets("5@23A23B.C")

if __name__ == "__main__": main()

Output

 Given text :  !*&3M'?an/<go_,j#u$$ic^e(&)
 Result     :  Mangojuice
 Given text :  5@23A23B.C
 Result     :  ABC
#    Ruby Program for
#    Remove all characters in a string except alphabet
class RemoveCharacters 
	#  Remove all characters other than alphabets from string
	def removeNonAlphabets(text) 
		#  Get the length
		n = text.length
		if (n == 0) 
			return
		end

		result = ""
		i = 0
		while (i < n) 
			if (text[i] >= 'a' && text[i] <= 'z' || 
                (text[i] >= 'A' && text[i] <= 'Z')) 
				#  When alphabets character
				result = result + text[i].to_s
			end

			i += 1
		end

		print(" Given text : ", text, "\n")
		print(" Result     : ", result, "\n")
	end

end

def main() 
	task = RemoveCharacters.new()
	#  Test
	task.removeNonAlphabets("!*&3M\'?an/<go_,j#u$$ic^e(&)")
	task.removeNonAlphabets("5@23A23B.C")
end

main()

Output

 Given text : !*&3M'?an/<go_,j#u$$ic^e(&)
 Result     : Mangojuice
 Given text : 5@23A23B.C
 Result     : ABC
import scala.collection.mutable._;
/*
    Scala Program for
    Remove all characters in a string except alphabet
*/
class RemoveCharacters()
{
	// Remove all characters other than alphabets from string
	def removeNonAlphabets(text: String): Unit = {
		// Get the length
		var n: Int = text.length();
		if (n == 0)
		{
			return;
		}
		var result: String = "";
		var i: Int = 0;
		while (i < n)
		{
			if (text.charAt(i) >= 'a' && text.charAt(i) <= 'z' || 
                (text.charAt(i) >= 'A' && text.charAt(i) <= 'Z'))
			{
				// When alphabets character
				result = result + text.charAt(i).toString();
			}
			i += 1;
		}
		println(" Given text : " + text);
		println(" Result     : " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: RemoveCharacters = new RemoveCharacters();
		// Test
		task.removeNonAlphabets("!*&3M\'?an/<go_,j#u$$ic^e(&)");
		task.removeNonAlphabets("5@23A23B.C");
	}
}

Output

 Given text : !*&3M'?an/<go_,j#u$$ic^e(&)
 Result     : Mangojuice
 Given text : 5@23A23B.C
 Result     : ABC
import Foundation;
/*
    Swift 4 Program for
    Remove all characters in a string except alphabet
*/
class RemoveCharacters
{
	// Remove all characters other than alphabets from string
	func removeNonAlphabets(_ data: String)
	{
      	let text = Array(data);
		// Get the length
		let n: Int = text.count;
		if (n == 0)
		{
			return;
		}
		var result: String = "";
		var i: Int = 0;
		while (i < n)
		{
			if (text[i] >= "a" && text[i] <= "z" || 
                (text[i] >= "A" && text[i] <= "Z"))
			{
				// When alphabets character
				result = result + String(text[i]);
			}
			i += 1;
		}
		print(" Given text : ", data);
		print(" Result     : ", result);
	}
}
func main()
{
	let task: RemoveCharacters = RemoveCharacters();
	// Test
	task.removeNonAlphabets("!*&3M\'?an/<go_,j#u$$ic^e(&)");
	task.removeNonAlphabets("5@23A23B.C");
}
main();

Output

 Given text :  !*&3M'?an/<go_,j#u$$ic^e(&)
 Result     :  Mangojuice
 Given text :  5@23A23B.C
 Result     :  ABC
/*
    Kotlin Program for
    Remove all characters in a string except alphabet
*/
class RemoveCharacters
{
	// Remove all characters other than alphabets from string
	fun removeNonAlphabets(text: String): Unit
	{
		// Get the length
		val n: Int = text.length;
		if (n == 0)
		{
			return;
		}
		var result: String = "";
		var i: Int = 0;
		while (i < n)
		{
			if (text.get(i) >= 'a' && text.get(i) <= 'z' || 
                (text.get(i) >= 'A' && text.get(i) <= 'Z'))
			{
				// When alphabets character
				result = result + text.get(i).toString();
			}
			i += 1;
		}
		println(" Given text : " + text);
		println(" Result     : " + result);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: RemoveCharacters = RemoveCharacters();
	// Test
	task.removeNonAlphabets("!*&3M\'?an/<go_,j#u$\$ic^e(&)");
	task.removeNonAlphabets("5@23A23B.C");
}

Output

 Given text : !*&3M'?an/<go_,j#u$$ic^e(&)
 Result     : Mangojuice
 Given text : 5@23A23B.C
 Result     : ABC

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment