📌  相关文章
📜  从字符串中删除除字母以外的所有字符

📅  最后修改于: 2022-05-13 01:57:07.228000             🧑  作者: Mango

从字符串中删除除字母以外的所有字符

给定一个由字母和其他字符组成的字符串,删除除字母之外的所有字符并打印这样形成的字符串。
例子:

Input : $Gee*k;s..fo, r'Ge^eks?
Output : GeeksforGeeks

Input : P&ra+$BHa;;t*ku, ma$r@@s#in}gh
Output : PraBHatkumarsingh

要删除除字母(az) && (AZ) 以外的所有字符,我们只需将字符与ASCII 值进行比较,对于值不在字母范围内的字符,我们使用字符串擦除函数删除这些字符。

C++
// CPP program to remove all the
// characters other then alphabets
#include 
using namespace std;
 
// function to remove characters and
// print new string
void removeSpecialCharacter(string s)
{
    for (int i = 0; i < s.size(); i++) {
         
        // Finding the character whose
        // ASCII value fall under this
        // range
        if (s[i] < 'A' || s[i] > 'Z' &&
            s[i] < 'a' || s[i] > 'z')
        {  
            // erase function to erase
            // the character
            s.erase(i, 1);
            i--;
        }
    }
    cout << s;
}
 
// driver code
int main()
{
    string s = "$Gee*k;s..fo, r'Ge^eks?";
    removeSpecialCharacter(s);
    return 0;
}


Java
// Java program to remove all the characters
// other then alphabets
 
class GFG
{
     
    // function to remove characters and
    // print new string
    static void removeSpecialCharacter(String s)
    {
        for (int i = 0; i < s.length(); i++)
        {
 
            // Finding the character whose
            // ASCII value fall under this
            // range
            if (s.charAt(i) < 'A' || s.charAt(i) > 'Z' &&
                    s.charAt(i) < 'a' || s.charAt(i) > 'z')
            {
                 
                // erase function to erase
                // the character
                s = s.substring(0, i) + s.substring(i + 1);
                i--;
            }
        }
        System.out.print(s);
    }
     
    // Driver code
    public static void main(String[] args)
    {
        String s = "$Gee*k;s..fo, r'Ge^eks?";
        removeSpecialCharacter(s);
    }
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program to remove all the
# characters other then alphabets
 
# function to remove characters and
# pr new string
def removeSpecialCharacter(s):
 
    i = 0
 
    while i < len(s):
 
        # Finding the character whose
        # ASCII value fall under this
        # range
        if (ord(s[i]) < ord('A') or
            ord(s[i]) > ord('Z') and
            ord(s[i]) < ord('a') or
            ord(s[i]) > ord('z')):
                 
            # erase function to erase
            # the character
            del s[i]
            i -= 1
        i += 1
 
    print("".join(s))
 
# Driver Code
if __name__ == '__main__':
    s = "$Gee*k;s..fo, r'Ge^eks?"
    s = [i for i in s]
    removeSpecialCharacter(s)
 
# This code is contributed by Mohit Kumar


C#
// C# program to remove all the characters
// other then alphabets
using System;
 
class GFG {
     
    // function to remove characters and
    // print new string
    static void removeSpecialCharacter(string s)
    {
        for (int i = 0; i < s.Length; i++)
        {
 
            // Finding the character whose
            // ASCII value fall under this
            // range
            if (s[i] < 'A' || s[i] > 'Z' &&
                    s[i] < 'a' || s[i] > 'z')
            {
                 
                // erase function to erase
                // the character
                s = s.Remove(i,1);
                i--;
            }
        }
         
        Console.Write(s);
    }
     
    // Driver code
    public static void Main()
    {
        string s = "$Gee*k;s..fo, r'Ge^eks?";
        removeSpecialCharacter(s);
    }
}
 
// This code is contributed by Sam007.


Javascript


C++
// CPP program to remove all the
// characters other then alphabets
#include 
using namespace std;
 
// function to remove characters and
// print new string
void removeSpecialCharacter(string s)
{
    int j = 0;
    for (int i = 0; i < s.size(); i++) {
         
        // Store only valid characters
        if ((s[i] >= 'A' && s[i] <= 'Z') ||
            (s[i] >='a' && s[i] <= 'z'))
        {
            s[j] = s[i];
            j++;
        }
    }
    cout << s.substr(0, j);
}
 
// driver code
int main()
{
    string s = "$Gee*k;s..fo, r'Ge^eks?";
    removeSpecialCharacter(s);
    return 0;
}


Java
// Java program to remove all the
// characters other then alphabets
 
class GFG {
 
// function to remove characters and
// print new string
    static void removeSpecialCharacter(String str) {
        char[] s = str.toCharArray();
        int j = 0;
        for (int i = 0; i < s.length; i++) {
 
            // Store only valid characters
            if ((s[i] >= 'A' && s[i] <= 'Z')
                    || (s[i] >= 'a' && s[i] <= 'z')) {
                s[j] = s[i];
                j++;
            }
        }
        System.out.println(String.valueOf(s).substring(0, j));
    }
 
// driver code
    public static void main(String[] args) {
        String s = "$Gee*k;s..fo, r'Ge^eks?";
        removeSpecialCharacter(s);
    }
}
 
// This code is contributed by 29AjayKumar


Python3
# Python program to remove all the
# characters other then alphabets
 
# Function to remove special characters
# and store it in another variable
def removeSpecialCharacter(s):
    t = ""
    for i in s:
         
        # Store only valid characters
        if (i >= 'A' and i <= 'Z') or (i >= 'a' and i <= 'z'):
            t += i
    print(t)
 
# Driver code
s = "$Gee*k;s..fo, r'Ge^eks?"
removeSpecialCharacter(s)
 
# This code is contributed by code_freak


C#
// C# program to remove all the
// characters other then alphabets
using System;
public class GFG {
  
// function to remove characters and
// print new string
    static void removeSpecialCharacter(String str) {
        char[] s = str.ToCharArray();
        int j = 0;
        for (int i = 0; i < s.Length; i++) {
  
            // Store only valid characters
            if ((s[i] >= 'A' && s[i] <= 'Z')
                    || (s[i] >= 'a' && s[i] <= 'z')) {
                s[j] = s[i];
                j++;
            }
        }
        Console.WriteLine(String.Join("",s).Substring(0, j));
    }
  
// driver code
    public static void Main() {
        String s = "$Gee*k;s..fo, r'Ge^eks?";
        removeSpecialCharacter(s);
    }
}
 //This code is contributed by PrinciRaj1992


Javascript


输出:

GeeksforGeeks

上述代码的时间复杂度为 O(n*n),因为在最坏的情况下,erase() 可能需要 O(n)。我们可以通过跟踪两个索引来优化解决方案。

C++

// CPP program to remove all the
// characters other then alphabets
#include 
using namespace std;
 
// function to remove characters and
// print new string
void removeSpecialCharacter(string s)
{
    int j = 0;
    for (int i = 0; i < s.size(); i++) {
         
        // Store only valid characters
        if ((s[i] >= 'A' && s[i] <= 'Z') ||
            (s[i] >='a' && s[i] <= 'z'))
        {
            s[j] = s[i];
            j++;
        }
    }
    cout << s.substr(0, j);
}
 
// driver code
int main()
{
    string s = "$Gee*k;s..fo, r'Ge^eks?";
    removeSpecialCharacter(s);
    return 0;
}

Java

// Java program to remove all the
// characters other then alphabets
 
class GFG {
 
// function to remove characters and
// print new string
    static void removeSpecialCharacter(String str) {
        char[] s = str.toCharArray();
        int j = 0;
        for (int i = 0; i < s.length; i++) {
 
            // Store only valid characters
            if ((s[i] >= 'A' && s[i] <= 'Z')
                    || (s[i] >= 'a' && s[i] <= 'z')) {
                s[j] = s[i];
                j++;
            }
        }
        System.out.println(String.valueOf(s).substring(0, j));
    }
 
// driver code
    public static void main(String[] args) {
        String s = "$Gee*k;s..fo, r'Ge^eks?";
        removeSpecialCharacter(s);
    }
}
 
// This code is contributed by 29AjayKumar

Python3

# Python program to remove all the
# characters other then alphabets
 
# Function to remove special characters
# and store it in another variable
def removeSpecialCharacter(s):
    t = ""
    for i in s:
         
        # Store only valid characters
        if (i >= 'A' and i <= 'Z') or (i >= 'a' and i <= 'z'):
            t += i
    print(t)
 
# Driver code
s = "$Gee*k;s..fo, r'Ge^eks?"
removeSpecialCharacter(s)
 
# This code is contributed by code_freak

C#

// C# program to remove all the
// characters other then alphabets
using System;
public class GFG {
  
// function to remove characters and
// print new string
    static void removeSpecialCharacter(String str) {
        char[] s = str.ToCharArray();
        int j = 0;
        for (int i = 0; i < s.Length; i++) {
  
            // Store only valid characters
            if ((s[i] >= 'A' && s[i] <= 'Z')
                    || (s[i] >= 'a' && s[i] <= 'z')) {
                s[j] = s[i];
                j++;
            }
        }
        Console.WriteLine(String.Join("",s).Substring(0, j));
    }
  
// driver code
    public static void Main() {
        String s = "$Gee*k;s..fo, r'Ge^eks?";
        removeSpecialCharacter(s);
    }
}
 //This code is contributed by PrinciRaj1992

Javascript


输出:

GeeksforGeeks

时间复杂度: O(n)