How to check several strings with operator ||(or) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to check several strings with operator ||(or)

I have several strings and I need to check for null with operator ||. For example: if (!String.IsNullOrEmpty(string1) || !String.IsNullOrEmpty(string2) || !String.IsNullOrEmpty(string3) || !String.IsNullOrEmpty(string4) || !String.IsNullOrEmpty(string5) || ...) { //something } Is there a more flexible way to do this?

13th May 2018, 6:56 PM
ADD AKU
ADD AKU - avatar
4 Answers
+ 3
Zakaria, it looks like a workaround. Have you got any idea? Is there some functions? It will be better without loops.
14th May 2018, 10:27 AM
ADD AKU
ADD AKU - avatar
+ 2
Brains thanks, but it's not that. 1. My strings have different names. 2. If I have some variables for iteration into my statement. For example: { b++; My_int++; } And after looping It will be increased several times instead of one time. b = 10 instead of b=1
13th May 2018, 7:19 PM
ADD AKU
ADD AKU - avatar
+ 1
for(int i=0;i<strings.length;i++){ if(!String.isNullorEmpty(strings[i])){ //do stuff } } All strings should be in an array
13th May 2018, 7:07 PM
᠌᠌Brains[Abidemi]
᠌᠌Brains[Abidemi] - avatar
+ 1
according to NG brains answer . you can do this . for(int i=0;i<strings.length;i++){ if(!String.isNullorEmpty(strings[i])){ //do stuff break; } } All strings should be in an array
13th May 2018, 8:01 PM
zakaria kasmi
zakaria kasmi - avatar