Unexpected results for checking if a character is a symbol

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Unexpected results for checking if a character is a symbol



I am creating a string extension to check if a string is all symbols or not however it is not working as I would expect it to, so far I have the following:


// 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())
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




Now if I use this extension on the following two strings I get the opposite of what I expect to see:


string testString = "!=";



I expect this to be all symbols, but


I expect: testString.ContainsOnlySymbols() => true
I get: testString.ContainsOnlySymbols() => false



Now if I use the next test string:


string testString = "Starts with";



I expect this to have no symbols


I expect: testString.ContainsOnlySymbols() => false
I get: testString.ContainsOnlySymbols() => true




4 Answers
4



A couple problems:



In your loop, you are really only getting the option related to the last character. And or clause should take care of it.


containsMore = containsMore || !(Char.IsSymbol(character) || Char.IsPunctuation(character));



Then, you need a not at the end. If it doesn't contain more, then its only symbols


return ! containsMore;



You might want a special case for how to handle empty strings too. Not sure how you want to handle that. That will be your choice if an empty string should return true or false.



You can accomplish this with a one-liner. See these examples.


string x = "@#=";
string z = "1234";
string w = "1234@";

bool b = Array.TrueForAll(x.ToCharArray(), y => (Char.IsSymbol(y) || Char.IsPunctuation(y))); // true
bool c = Array.TrueForAll(z.ToCharArray(), y => (Char.IsSymbol(y) || Char.IsPunctuation(y))); // false
bool e = Array.TrueForAll(w.ToCharArray(), y => (Char.IsSymbol(y) || Char.IsPunctuation(y))); // false





Yes, this is right, I made a stupid mistake and forgot return !containsMore, I realized this but saw you put it as the answer so thanks
– G. LC
Aug 3 at 12:47





@G.LC I don't think "!=" will return true for you with this answer... Look at my anwser
– Thierry V
Aug 3 at 12:55





What I ended up doing to fix that was just use @asantaballa's answer with the Char.Punction piece, I will update my question to show this
– G. LC
Aug 3 at 13:59





ok..i updated the answer accordingly...i have to fix it...fixed
– Ctznkane525
Aug 3 at 14:04




Checking all chars if all isSymbol or Punctuation. we return true here.


public static bool ContainsOnlySymbols(this String inputString)

return inputString.ToCharArray().All(x => Char.IsSymbol(x)



Test:


string testString = "Starts with"; // => false
string testString = "!="; // => true
string testString = "@@"; // => true
string testString = "!Starts with"; // => false





Whoops I forgot that part that in my question, it still does not work, I edit my question
– G. LC
Aug 3 at 12:33





can you try this answer? it should works. @G. LC
– arslanaybars
Aug 3 at 12:34






Yeah Ill try it, give me a sec
– G. LC
Aug 3 at 12:35





Thanks for your answer dude, but I figured it out, ill add an answer, it is an obvious mistake we all missed
– G. LC
Aug 3 at 12:46





I saw the issue now. we should check as All() instead of Any().
– arslanaybars
Aug 3 at 12:52



I believe the IsSymbol method checks for a very specific set of character. You may want to do:


containsMore = (Char.IsSymbol(character) || Char.IsPunctuation(character)) ? false : true;



Wrote a quick program to show results for character and does show symptom. Might even be that all you need for your app is IsPunctuation.


33/!: IsSymbol=False, IsPunctuation=True



Program


using System;

namespace csharptestchis

class Program

static void Main(string args)

for (int i = 0; i <= 255; i++)

char ch = (char)i;
bool isSymbol = Char.IsSymbol(ch);
bool isPunctuation = Char.IsPunctuation(ch);
Console.WriteLine($"i/ch: IsSymbol=isSymbol, IsPunctuation=isPunctuation ");








Ok let me check if I can find which symbols it checks for, I thought of this but didn't try it, I'll test it out
– G. LC
Aug 3 at 12:39



Firstly, the idea is simple: you loop your string, if you meet a character non-symbol, return false. Until the end of string and you don't meet a character non-symbol. Voilà, return true.


string


false


string


true


public static bool ContainsOnlySymbols(string inputString)

// Identifiers used are:
bool containsMore = false;

// Go through the characters of the input string checking for symbols
foreach (char character in inputString)

containsMore = Char.IsSymbol(character) ? false : true;
if(!containsMore)
return false;


// Return the results
return true;



Secondly, there is a problem with your code, IsSymbol returns true only if your character is in these groups


IsSymbol



MathSymbol, CurrencySymbol, ModifierSymbol, and OtherSymbol.



And fortunately, ! don't be in these groups. That means "!=" returns false.


!


false



So you must include others conditions like:


public static bool ContainsOnlySymbols(string inputString)
Char.IsPunctuation(c));



Or you have to write your own method to determine what symbol is acceptable and what is not.



Or if a string doesn't contain digit and letter, it can be considered symbol. You can do


public static bool ContainsOnlySymbols(string inputString)

// Go through the characters of the input string checking for symbols
return !inputString.Any(c => Char.IsLetterOrDigit(c));





Calling ToCharArray is not necessary here as string implements IEnumerable<char>;
– Chris Dunaway
Aug 3 at 15:02


ToCharArray


string


IEnumerable<char>





@ChrisDunaway thanks, answer updated
– Thierry V
Aug 3 at 15:06






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS