我有一个字符串需要转换为
第二个示例会因以下代码而崩溃。
具体来说,当调用
String[]
,其中每个单词都在字符串中。但是,我不需要任何空格或任何标点符号,除了属于单词的连字符和撇号。
示例输入:
Hello! This is a test and it's a short-er 1. - [ ] { } ___)
从输入中生成的数组示例:
[ "Hello", "this", "is", "a", "test", "and", "it's", "a", "short-er", "1" ]
目前,这是我尝试过的代码:
(注意:当调用string.First()
时,第二个代码会导致错误):
private string[] ConvertWordsFromFile(String NewFileText)
{
char[] delimiterChars = { ' ', ',', '.', ':', '/', '|', '<', '>', '/', '@', '#', '$', '%', '^', '&', '*', '"', '(', ')', ';' };
string[] words = NewFileText.Split(delimiterChars, StringSplitOptions.RemoveEmptyEntries);
return words;
}
或者
private string[] ConvertWordsFromFile(String NewFileText)
{
return Regex.Split(NewFileText, @"\W+");
}
第二个示例会因以下代码而崩溃。
private string GroupWordsByFirstLetter(List<String> words)
{
var groups =
from w in words
group w by w.First();
return FormatGroupsByAlphabet(groups);
}
具体来说,当调用
w.First()
时。