Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
4.33/5 (3 votes)
See more:
excuse my silly question
i want to read 3 variables x,y and z ,they are written in one horizontal line in text file "myfile.txt"
like this
123 456 7890
i have read the string by
myString=MyStreamReader.ReadLine();
there are spaces between the variables ,i do not know how many spaces between the variable
x and y , and y and z
if guss
myString.Split will not works(if Split can do the job please let me know how),i also will not prefere the
myString.Substring(id,len)
because the length of each variable is unpredected
Posted

String.Split works, provide you specify the RemoveEmptyEntries option, for instance:

C#
string str = "123 456          7890";
string[] snum = str.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries);
int[] v = new int[snum.Length];
for (int n = 0; n < v.Length; n++ )
{
    v[n] = int.Parse(snum[n]);
}
 
Share this answer
 
v3
Comments
nagendrathecoder 18-Feb-11 8:15am    
This is much better option. :), 5 to you.
Sergey Alexandrovich Kryukov 18-Feb-11 8:37am    
Passed, my 5.
--SA
CPallini 18-Feb-11 8:56am    
:-D
fjdiewornncalwe 18-Feb-11 20:19pm    
Exactly correct. +5.
Try:
string[] numbers = myString.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Feb-11 8:36am    
Enough said, I suggest, my 5.
--SA
If there are spaces for sure between variables, you can split that string with space and than you can use only those strings whose value is not ''.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Feb-11 8:36am    
Don't teach sloppy coding. For future, you should use string.Empty instead of "", to avoid hard-coded immediate constants, even this one, set aside StringSplitOptions.RemoveEmptyEntries.
--SA

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