3

Hi for example i have a String in sinhala unicode

 String variable = "සහ"; - Correct String
 String variable = "XසහS"; - Incorrect String

I want to validate if ALL the characters string is in the specific unicode range which belongs to that specific language.

Any idea of achiving this ?

Lucas
  • 3,376
  • 6
  • 31
  • 46
Sudantha
  • 15,684
  • 43
  • 105
  • 161
  • @Patrick: Not correct. `var` is a *contextual* keyword. Because of that, `var` is still valid as a variable name. Otherwise, when `var` was introduced to the language, it might have broken code. – jason Jul 21 '13 at 14:52
  • Just a note to say that checking whether all characters in a string fall within a specified Unicode block range is not the same as determining whether the string is well-formed. The dependent vowels in sinhala need to follow a consonant character, for example. – Tim Jul 21 '13 at 16:27

2 Answers2

1
static bool Validate(string s, char max, char min)
{
    for (int i = 0; i < s.Length; i++)
        if (s[i] > max || s[i] < min)
            return false;
    return true;
}
Denis
  • 5,894
  • 3
  • 17
  • 23
  • I think the problem is determining `min` and `max`. And then there's the other problem of it not necessarily being a *single* range. – jason Jul 21 '13 at 14:54
0

Ghosts's function in a single line:

static bool Validate(string s, char max, char min) {
    return s.All(c => min <= c && c <= max);
}
Steve
  • 6,334
  • 4
  • 39
  • 67
  • First, your answer should just be a comment or edit of the original. If you're going to do it in a single line, you really should say `!s.Any(c => c > max || c < min)` or `s.All(c => min <= c && c <= max)`. You don't want to know the *count* of the number of characters outside the range, you want to know if there are *any* characters outside the range or if *all* of the characters are inside the range. But, this problem is [not that simple](http://stackoverflow.com/questions/17773531/c-sharp-how-to-check-if-strings-all-characters-in-a-specific-unicode-range/17773577#comment25922535_17773577). – jason Jul 21 '13 at 14:56
  • You're right about using `Any()` or `All()`, I've updated the answer. Also, see http://www.unicode.org/charts/PDF/U0D80.pdf - Sinhalese script was added to the Unicode Standard in September 1999 with the release of version 3.0. The Unicode block for Sinhala is U+0D80–U+0DFF. Grey areas indicate non-assigned code points. (via Wikipedia) – Steve Jul 21 '13 at 15:01