我正在创建一个字符串扩展来检查一个字符串是否都是符号,但是它没有像我期望的那样工作,到目前为止我有以下内容:
// Class for: String extensions
public static class StringExtension
{
// Method for: Determining if a string contains only symbols
public static bool ContainsOnlySymbols(this String inputString)
{
// Identifiers used are:
bool containsMore = false;
// Go through the characters of the input string checking for symbols
foreach (char character in inputString.ToCharArray())
{
// This line needs || Char.IsPunctuation(character) also
// Credit: @asantaballa
containsMore = Char.IsSymbol(character) ? false : true;
if (containsMore)
{
return containsMore;
}
}
// Return the results
return containsMore; // Edited after answer: <-- mistake here
}
}
现在,如果我在以下两个字符串上使用此扩展名,则会得到与我期望看到的相反的结果:
string testString = "!=";
我希望这都是符号,但是
I expect: testString.ContainsOnlySymbols() => true
I get: testString.ContainsOnlySymbols() => false
现在,如果我使用下一个测试字符串:
string testString = "Starts with";
我希望这没有符号
I expect: testString.ContainsOnlySymbols() => false
I get: testString.ContainsOnlySymbols() => true
慕娘9325324
MMMHUHU
墨色风雨
相关分类