-1

** I wanted to check a sentence whether contains one null character between two words. I am new. So, there is not much of code samples for This problem. **

I used String.IsNullOrEmpty to determine the emptiness of the string. However, I did wrong.

Can you help me about that?

**Edit: I'm sorry for My mistake. This question needs an example. For example; I Write to the textbox "It is good" It determines null characters between "It" and "is" AND "is" and "good". So, it gives an error message. However, İf I Write one single character, it does not give me error message.

PS: This error message means a label. If it contains a null character, Red label shows itself. Else, Green label shows itself.**

Edit 2

Public Static bool IsAllLetters(string s) 

{ foreach (char c in s) { if (!char.IsLetter(c) return false; return true; }

I determine whether the string contains Letter or not. If it contains a number character, it gives error.

Anyway, that explains why I used IsAllLetter function.

then I used This code samples.

Bool exp = IsAllLetters(explanation_text.Text);

İf (exp == false){ // wrong data } 

Else { // correct data } 

So, Which Code should I change? Or What Code should I add?

1 Answers1

0

Your code formatting is a bit rough, but it looks like your foreach returns prematurely when the string has Length greater than 1

foreach (char c in s)
   { if (!char.IsLetter(c) return false; return true; }

The code above only ever checks the first character of a string. Instead, you want IsAllLetters to only return true if all characters have been scanned.

public static bool IsAllLetters(string s)
{
    foreach (char c in s)
        if (!char.IsLetter(c)) return false;

    return true;
}
RamblinRose
  • 4,883
  • 2
  • 21
  • 33