Click here to Skip to main content
15,396,210 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a string . I want to check if it contains data.

If there is data the string will look like this - "1,aml,007"

If there is no data the string looks like this - ",," (how to give validation to exit in this scenario)

Dim fileStrg as new String
Dim Sreader as StreamReader

fileStrg = sreader.ReadLine // gets data from comma separated file

if(what validation) then
Exit function

//Process the data

End if

What I have tried:

I tried splitting the string into a string array. As a beginner I'm not understanding how to traverse between the string array and exit out.
And it's simply complicating code.
Updated 18-Nov-21 1:15am

1 solution

It's simple to check: just split the string as you have done, then loop through the array comparing each element with an empty string (I'd probably use Trim on each before the comparison). If you don't find any data, ignore the line (or exit the method, your choice).

To be honest, if you can't loop through an array you probably shouldn't be playing with files yet - and CSV data can get a lot more complicated than just "it's spearated by commas": all of this is legal:
"hello", "hello, world", "hello, ""world"""
And contains three fields:
hello, world
hello, "world"
Simplistic processing of CSV data by just splitting on commas will come back to bite you later, and hard.

I'd suggest you try using some of the existing CSV reading software, as it is already aware of the problems. I use this one: A Fast CSV Reader[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900