Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter a String

I want to make sure a string has only characters in this range

[a-z] && [A-Z] && [0-9] && [-]

so all letters and numbers plus the hyphen. I tried this...

C# App:

        char[] filteredChars = { ',', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '=', '{', '}', '[', ']', ':', ';', '"', '\'', '?', '/', '.', '<', '>', '\\', '|' };
        string s = str.TrimStart(filteredChars);

This TrimStart() only seems to work with letters no otehr characters like $ % etc

Did I implement it wrong? Is there a better way to do it?

I just want to avoid looping through each string's index checking because there will be a lot of strings to do...

Thoughts?

Thanks!

like image 926
Gabe Avatar asked May 25 '09 20:05

Gabe


People also ask

Can you filter a string?

We can also use filter() with a string as an iterable sequence and can filter out characters from it.

Can you filter a string in JS?

JavaScript – Filter Strings of an Array based on Length filter() method returns an array with elements from the original array that returns true for the given callback function.

How do you filter a string in Python?

filter() method is a very useful method of Python. One or more data values can be filtered from any string or list or dictionary in Python by using filter() method. It filters data based on any particular condition. It stores data when the condition returns true and discard data when returns false.


5 Answers

This seems like a perfectly valid reason to use a regular expression.

bool stringIsValid = Regex.IsMatch(inputString, @"^[a-zA-Z0-9\-]*?$");

In response to miguel's comment, you could do this to remove all unwanted characters:

string cleanString = Regex.Replace(inputString, @"[^a-zA-Z0-9\-]", "");

Note that the caret (^) is now placed inside the character class, thus negating it (matching any non-allowed character).

like image 82
Tomas Aschan Avatar answered Sep 30 '22 12:09

Tomas Aschan


Here's a fun way to do it with LINQ - no ugly loops, no complicated RegEx:

private string GetGoodString(string input)
{
   var allowedChars = 
      Enumerable.Range('0', 10).Concat(
      Enumerable.Range('A', 26)).Concat(
      Enumerable.Range('a', 26)).Concat(
      Enumerable.Range('-', 1));

   var goodChars = input.Where(c => allowedChars.Contains(c));
   return new string(goodChars.ToArray());
}

Feed it "Hello, world? 123!" and it will return "Helloworld123".

like image 41
Judah Gabriel Himango Avatar answered Sep 30 '22 11:09

Judah Gabriel Himango


Why not just use replace instead? Trimstart will only remove the leading characters in your list...

like image 37
miguel Avatar answered Sep 30 '22 12:09

miguel


Try the following

public bool isStringValid(string input) {
  if ( null == input ) { 
    throw new ArgumentNullException("input");
  }
  return System.Text.RegularExpressions.Regex.IsMatch(input, "^[A-Za-z0-9\-]*$");
}
like image 34
JaredPar Avatar answered Sep 30 '22 12:09

JaredPar


I'm sure that with a bit more time you can come up wiht something better, but this will give you a good idea:

public string NumberOrLetterOnly(string s)
{
    string rtn = s;
    for (int i = 0; i < s.Length; i++)
    {
        if (!char.IsLetterOrDigit(rtn[i]) && rtn[i] != '-')
        {
            rtn = rtn.Replace(rtn[i].ToString(), " ");
        }
    }
    return rtn.Replace(" ", "");
}
like image 31
Joel Avatar answered Sep 30 '22 13:09

Joel