Extension (IsCharacterString()) give false state when I enter space or tap #4666
Replies: 10 comments
-
Hello AbdAlghaniAlbiek, thank you for opening an issue with us! I have automatically added a "needs triage" label to help get things started. Our team will analyze and investigate the issue, and escalate it to the relevant team if possible. Other community members may also look into the issue and provide feedback 🙌 |
Beta Was this translation helpful? Give feedback.
-
@AbdAlghaniAlbiek can you please provide more detail on this? The steps you are taking or add a screenshot would be great. |
Beta Was this translation helpful? Give feedback.
-
I hope that but github doesn't support uploading images sting testText = Hello Kyaa; ==> Not valid you can see that there isn't numbers or operators it should be valid but when I enter on space or tap it give me Not valid |
Beta Was this translation helpful? Give feedback.
-
@AbdAlghaniAlbiek is that the code you are using? Seems like 'Writeline' is missing an e in the third line and string in first? |
Beta Was this translation helpful? Give feedback.
-
No this is just example I have written on github |
Beta Was this translation helpful? Give feedback.
-
@AbdAlghaniAlbiek That makes sense. Thanks for clarifying 👍 |
Beta Was this translation helpful? Give feedback.
-
@AbdAlghaniAlbiek the purpose of this method was to validate letter characters only (a 'space' is not a letter); it doesn't accept spaces as valid input as you've discovered by design. The RegEx driving this is here: If you'd like, you could open a PR to add an additional method to accept whitespace like: public static bool ContainsCharactersOrWhiteSpace(this string str) ... We should probably deprecate @Sergio0694 curious on your optimization expertise for these cases? (especially as these aren't really localized as well... though larger changes to the behavior should probably go in the 7.0 release) |
Beta Was this translation helpful? Give feedback.
-
@michael-hawker If the regex is just that, you can replace that with a linear search and a comparison of the value of each character, to make sure it's within the valid range. public static bool ContainsCharactersOrWhiteSpace(string text)
{
foreach (char c in text)
{
if (c == ' ' ||
unchecked((uint)c - 'A') <= 'Z' ||
unchecked((uint)c - 'a') <= 'z')
{
return false;
}
}
return true;
} I'm using
This trick lets us skip two conditional branches, as we can check each range with a single branch. This is a similar optimization to one of those added to .NET 5 (you can read the blog post here). That Hope this helps! 😄 |
Beta Was this translation helpful? Give feedback.
-
Thanks @Sergio0694, figured you'd have some great tips. Think it could be a good solution for now, but am still curious on the localization bit and the meaning of 'character'. Would be interested to know what some other folks think about these types of methods. It'd be good to hear some scenarios of where this is being used. @AbdAlghaniAlbiek can you share your scenario with us? What are you trying to accomplish? |
Beta Was this translation helpful? Give feedback.
-
In my way I split the text that I want to test it to List of strings and check each subtext |
Beta Was this translation helpful? Give feedback.
-
In Extensions session (IsCharacterString()) when I enter space or tap in textbox that check if all characters inside it are only letters not numbers or operators -> give me false state such as:
textbox.Text = Howareyou? -> true
textbox.Text = How are you? -> false
Beta Was this translation helpful? Give feedback.
All reactions