Split()

Signo.X

Well-known member
Joined
Aug 21, 2006
Messages
76
Location
Australia
Programming Experience
1-3
hello all,
im trying to read a text file , which has a data similar to the following :

123,c1p01
.
.
.
i need to check that for every line it has 3 things :

1. the line contains 2 values seperated by a comma ","
2.the value befor the comma is numeric
3. the value after the comma should have at least 1 numeric value.

thats what i have so far..

VB.NET:
Dim strDir AsString = "C:\temp\test.txt"
Dim s AsString
Try
Dim sr AsNew System.IO.StreamReader(strDir)
DoUntil sr.Peek < 0
s = sr.ReadLine
 
If (s.Contains(",")) Then [COLOR=darkgreen]' check if the line contains a comma[/COLOR]
 
Console.WriteLine(s)
 
EndIf
 
Loop
sr.Close()
Catch ex As Exception
Console.WriteLine(ex.Message)
EndTry
EndSub
i was trying to use the method split..but i still cant get what i need to do.:mad:

can you please help me asap, as im new to vb.net ...

thanks in advance
 
Last edited by a moderator:
You should read the line into a string, then call Split on that string to create a string array. If the array has two elements you know there was a comma present. Now you use Double.TryParse to try to convert the first element to a Double. If that works then you're two out of three. Use Integer.TryParse if you don't want to allow fractiuonal values. Finally, you'll just have to feed every character in the second element to Char.IsDigit and if one returns true then you're three for three.
 
Thanks heaps for you help, Indeed it saved alot of time.
now im stuck at the 3 point. i.e. the second value should contain at least one number..
here is the code that i have so far..

VB.NET:
Try
Dim sr AsNew System.IO.StreamReader(strDir)
Dim temp() AsString
Dim i AsInteger = 0
DoUntil sr.Peek < 0
s = sr.ReadLine
IfNot (s.Equals("") OrString.IsNullOrEmpty(s)) Then'ignore white spaces
If (s.Contains(",")) Then' check if it has 2 a comman, hence 2 values
temp = s.Split(",")
If (Integer.TryParse(temp(0), 0) = False) Then'check if the first value an integer
Console.WriteLine("Failuer...Exit")
Console.WriteLine(temp(0))
ExitDo
Else
Console.WriteLine("Success...")
Console.WriteLine(temp(i))
EndIf
Else
Console.WriteLine("exit sub, the file has no comma, immediate failuer")
ExitDo
EndIf
Else
Console.WriteLine("Empty/Null line ignored")
EndIf
 
 
Loop
sr.Close()
and here is sample of the text file that im using..
1 ,c10b
2 ,chkdd
kk3, c303

when im using the split method, its only returning the first part before the comma and ignores the 2nd part...can u help me with that please? and show me a code snippet based on what i started?
 
Last edited by a moderator:
Split does not ignore the part after the comma. It creates an array of substrings by splitting on the comma. If there is one comma then the array will have two elements, so to get them both you have to get the elements at index 0 and index 1.
 
yeah i know
but from the code i posted earlier, i was trying to say why is it only returning the values before the comma and NOT after ?
i still have a problem with that..
 
You mean this: temp(i) ?
Your i variable is zero here and returns the first element in array.
 
hello all,
im trying to read a text file , which has a data similar to the following :

i need to check that for every line it has 3 things :

1. the line contains 2 values seperated by a comma ","
2.the value befor the comma is numeric
3. the value after the comma should have at least 1 numeric value.

Check out FileInfo and in particular the OpenText or OpenRead methods for reading text files..

As for your 3 points, the following line of code solves all 3:

If Regex.IsMatch(input_line, "[0-9]+[,].*[0-9].*")


The regex decoded:
"[0-9]+[,].*[0-9].*"

[0-9]a character in the range 0 to 9 (see charmap.exe)
+one or more occurrences of

[,]a literal comma

.any character
*zero or more occurrences of

[0-9]a character in the range 0 to 9

.any character
*zero or more occurrences of

so reading in english:
a sequence of at least 1 at most infinite numeric characters followed by a comma followed by zero or more of anything followed by a single numeric followed by zero or more of anything at all
 
The problem has been solved..

i've used the split method, lots of code .. but yeah did the job perfectely..i think it was more of a logic problem for me rather than a syntax problem..

coming from java and c++ .. i found that vb.net is way more friendley, too many different ways of doing the same thing which makes me confused some times..,

back in the days, we use to use note pad to write the code and run it in the dos shell..but now, thanks to the almighty VS.NET 2005 .. makes life more fun..


thank you all for helping ~
:D
 
coming from java and c++ .. i found that vb.net is way more friendley, too many different ways of doing the same thing which makes me confused some times..,

there will always be more than one way to skin a particular cat. if youre confused as to the best route to take, be sure to know we'd welcome the opportunity to guide you.. one particualr bit of guidance i'd give is to try as much as possible to avoid using methods and classes within the Microsoft.VisualBasic namespace as they are specific to VB.NET - coming from c++ and java youre in a great position to adopt a working knowledge of all the .NET languages, c++, c#, j# and VB.NET. Syntactically, VB.NET is the most far removed from the others, and some things are a bit quirky and inconsistent, though they are indeed as you say "more friendly". Take a look at the other .NET syntaxes - gaining an appreciateion of .NET as a framework rather than a particular syntax will stad you in good stead commercially; any one of the top VB.NET programmers here could easily do a job advertised as C# only, if they had a working knowledge of the syntax.. given that you may already know [most of] it, dont lose it! :)
 
Back
Top