1

I want to find a way to check if a string contains text and if it does it will go to to the next one and keep doing this till it finds an empty string or reached the end.

The issue is I can't find anything that I could use to check if the string is containing any text, I can only find if it a IsNullOrWhiteSpace or if it contains a specific text.

5
  • So it does not have to contain a specific text, ist just must not be empty? Commented Dec 24, 2019 at 8:40
  • 1
    So, why you can't use IsNullOrWhiteSpace ? Commented Dec 24, 2019 at 8:41
  • 2
    What have you tried so far? What is your code? Have you tried IsNullOrWhiteSpace or regex? Please take a look at How to ask a good question and/or How to create minimal reproducible example Commented Dec 24, 2019 at 8:42
  • I have not tried anything as of yet has I can't find any sort of function that will do what what I want. Best example to give is think of a diary and you want to make a appointment. so when booking that appointment it will go through every date till it finds a open slot. So if it does contained text it will skip to the next day and then the next till it finds a string that does not contain text. Commented Dec 24, 2019 at 8:48
  • If a string contains any text then it is NOT "null or whitespace" - you can invert a test with the ! (not) operator Commented Dec 24, 2019 at 8:56

1 Answer 1

6

When does a string contain text? Well when the string exists and it does not contain an empty text. When does a string contain an empty text? When the length of the string is 0.

So, answering your question, a text is not empty when it exists and s.Length != 0:

if (s != null && s.Length > 0) { /*s is not empty*/ }

or better yet

if (s?.Length > 0) { /*s is not empty*/ }

or if you prefer a string contains text when it is not nonexistant or empty:

if (!string.IsNullOrEmpty(s)) { /*s is not empty*/ }

Now if texts consisting only of whitespaces must also be considered as empty, then when is a text not empty? When the text is anything but nonexistant or empty spaces, that is, IsNullOrWhiteSpace is false:

if (!string.IsNullOrWhiteSpace(s)) { /*s is not empty*/ }
Sign up to request clarification or add additional context in comments.

1 Comment

if (!string.IsNullOrEmpty(s)) { /*s is not empty*/ } This is what I needed thank you so much! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.