Hello!
I am trying to parse lines of text that are not tab or comma delimited. Once I have loaded the lines into an array I need to grab a number from the line to be able to use for another command. The line would look like this:
rdp-tcp#142 newuser 1 Active rdpwd
And I would need the 1 (or whatever integer is there). The spaces from the user to that may not always be the same (due to username).
How can I do this? Thanks much inadvance. Love this site!
Loading
MartinPosted Jan 19, 2007, 4:00 AM
From what I understand from the above, there will be something like:
If the above is correct, and the assumption that nothing apart from the seperater can contain spaces, then you can just do something along the lines of:
string yourString = "rdp-tcp#142 newuser 1 Active rdpwd";
int location = 0;
char[] array = yourString.ToCharArray();
string code = "";
string userName = "";
string integer = "";
string active = "";
string something = "";
for (int i = 0; i < array.GetLength(0); i++)
{
// Test if the next character is a whitespace
if (array[i] == ' ')
{
location++;
while (array[i] == ' ')
i++;
}
switch (location)
{
case 0:
code += array[i];
break;
case 1:
userName += array[i];
break;
case 2:
integer += array[i];
break;
case 3:
active += array[i];
break;
case 4:
something += array[i];
break;
default:
break;
}
}
Of course, that's a very ugly way to do it, another would be as simple as:
while (yourString.Contains("  ")) // This is a double space, don't let this forum fool you
yourString = yourString.Replace("  ", " ");
And then just split it on a single space.
Scott LyslePosted Jan 18, 2007, 9:30 PM
JackPosted Jan 18, 2007, 6:31 PM
Scott LyslePosted Jan 18, 2007, 7:09 AM
You said that the file is not comma or tab delimited; and that the number of spaces between fields varied. Is not the file fixed width, e.g., is the first field is always from 0 to 17, the second 18 to 42, etc.