Home > Back-end >  How can I identify special characters in a string? C#
How can I identify special characters in a string? C#

Time:10-24

I'm trying to get all characters in a string that occur before a certain character.

For example:

string id = "Hello everyone! Good day today.";
id = id.Remove(id.IndexOf('!'));
Console.WriteLine(id);
//output will be "Hello everyone"

And this works, but when I'm reading text from a special file and working with strings that contain special characters, I run into some trouble.

For example:

string id = "Hello everybodyø1Ø9ß&ëÄ"

If I wanted to only get the "Hello everybody" text, how would I do that? These special characters can be anything.

CodePudding user response:

A simple way would be to specify the allowed chars and use Enumerable.TakeWhile:

string id = "Hello everybodyø1Ø9ß&ëÄ";
string allowedChars = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
string result = string.Concat(id.TakeWhile(allowedChars.Contains)); // Hello everybody

Or use Regex as shown here(which just checks if there are special chars).

CodePudding user response:

Try this:

        string[] KeepCharacters = { "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"," ", "~","`","!","@","#","$","%","^","&","*","(",")","-","_"," ","=",@"\","|","}","]","{","[","'",":",";","/","?",".",">",",","<","1","2","3","4","5","6","7","8","9","0", Convert.ToString((char)34)};
        string id = "Hello everybodyø1Ø9ß&ëÄ";

        for(int i =0; i < id.Length; i  )
        {
            if(KeepCharacters.Contains(id.Substring(i,1).ToLower()) == false)
            {
                id = id.Remove(i);
                break;
            }
        }

CodePudding user response:

You could create an extension method added to string and use it as you wish.

Also note that I used StringBuilder since it doesn't create a new object in the memory but dynamically expands memory to accommodate the modified string (in contrast of doing it via regular string concatenation which could effect performance on long strings)

public static string GetRegularString(this string str) {
   StringBuilder sb = new StringBuilder();
   foreach (char c in str) {
      if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '.' || c == '_' || c == ' ') {
         sb.Append(c);
      }
      else{
        break;
      }
   }
   return sb.ToString();
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

I looked into it and found my own solution that works like a charm

id = id.Remove(id.IndexOf(id.Where(x => !char.IsLetterOrDigit(x)).First()));
  • Related