Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing Specified Punctuation From Strings

Tags:

string

c#

regex

I have a String that in need to convert into a String[] of each word in the string. However I do not need any white space or any punctuation EXCEPT hyphens and Apostrophes that belong in the word.

Example Input:

Hello! This is a test and it's a short-er 1. - [ ] { } ___)

Example of the Array made from Input:

[ "Hello", "this", "is", "a", "test", "and", "it's", "a", "short-er", "1" ]

Currently this is the code I have tried

(Note: the 2nd gives an error later in the program when string.First() is called):

private string[] ConvertWordsFromFile(String NewFileText)
{
     char[] delimiterChars = { ' ', ',', '.', ':', '/', '|', '<', '>', '/', '@', '#', '$', '%', '^', '&', '*', '"', '(', ')', ';' };
     string[] words = NewFileText.Split(delimiterChars, StringSplitOptions.RemoveEmptyEntries);
     return words;
}

or

private string[] ConvertWordsFromFile(String NewFileText)
{     
    return Regex.Split(NewFileText, @"\W+");
}

The second example crashes with the following code

private string GroupWordsByFirstLetter(List<String> words)
{
    var groups =
        from w in words
        group w by w.First();
    return FormatGroupsByAlphabet(groups);
}

specifically, when w.First() is called.

like image 804
James Thompson Avatar asked Sep 28 '26 11:09

James Thompson


2 Answers

To remove unwanted characters from a String

string randomString = "thi$ is h@ving s*me inva!id ch@rs";
string excpList ="$@*!";

LINQ Option 1

var chRemoved = randomString
                  .Select(ch => excpList.Contains(ch) ? (char?)null : ch);

var Result = string.Concat(chRemoved.ToArray());    

LINQ Option 2

var Result = randomString.Split().Select(x => x.Except(excList.ToArray()))
                                      .Select(c => new string(c.ToArray()))
                                      .ToArray();
like image 141
Prabhu Murthy Avatar answered Sep 30 '26 01:09

Prabhu Murthy


Here is a little something I worked up. Splits on \n and removes any unwanted characters.

    private string ValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789'-";
    private IEnumerable<string> SplitRemoveInvalid(string input)
    {
        string tmp = "";
        foreach(char c in input)
        {
            if(c == '\n')
            {
                if(!String.IsNullOrEmpty(tmp))
                {
                    yield return tmp;
                    tmp = "";
                }
                continue;
            }
            if(ValidChars.Contains(c))
            {
                tmp += tmp;
            }
        }
        if (!String.IsNullOrEmpty(tmp)) yield return tmp;
    }

Usage could be something like this:

    string[] array = SplitRemoveInvalid("Hello! This is a test and it's a short-er 1. - [ ] { } _)")
                     .ToArray();

I didnt actually test it, but it should work. If it doesnt, it should be easy enough to fix.

like image 22
caesay Avatar answered Sep 30 '26 00:09

caesay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!