processing 1;2;3;4 to i[0]=1 , i[1]=2 and......

  • Thread starter Thread starter Jeroen Ceuppens
  • Start date Start date
J

Jeroen Ceuppens

Hi, i put some data in xml, i get the data out of the xml and put it in a
string
string data= "1;2;3;4"

So i need something that do the ; away and puts every number into the array
i:

1 goes to i[0]
2 goes to i[1]

and so on........

How can i do this?

Greetz
JC
 
Do you mean sth. like:
string _content = "1;2;3"

int _nPos = _content.IndexOf(";");

int i = 0;

while ( _nPos > 0 )

{

myArray[i++] = int.Parse(_content.Substring(0, _nPos ));

_content = _content.Substring(_nPos+1);

_nPos = _content.IndexOf(";");

}



Ruediger
 
Yes, thx, but there is a little problem, the last number (3), isn't in the
array, so how can i fix it that it is add to the array?

Thx
JC
Rüdiger Kardel said:
Do you mean sth. like:
string _content = "1;2;3"

int _nPos = _content.IndexOf(";");

int i = 0;

while ( _nPos > 0 )

{

myArray[i++] = int.Parse(_content.Substring(0, _nPos ));

_content = _content.Substring(_nPos+1);

_nPos = _content.IndexOf(";");

}



Ruediger

Jeroen Ceuppens said:
Hi, i put some data in xml, i get the data out of the xml and put it in a
string
string data= "1;2;3;4"

So i need something that do the ; away and puts every number into the array
i:

1 goes to i[0]
2 goes to i[1]

and so on........

How can i do this?

Greetz
JC
 
JC,

i'm not sure if it is possible to change the size of an array at runtime (i
believe you can't).
use arraylist and addrange() instead of array.

Ruediger
 
Jergen,

You could also use String.Parse to separate the parts of the string. You
could estimate the size of array you'd need from the length of the string.
--
Ginny Caughey
..Net Compact Framework MVP

Jeroen Ceuppens said:
Yes, thx, but there is a little problem, the last number (3), isn't in the
array, so how can i fix it that it is add to the array?

Thx
JC
Rüdiger Kardel said:
Do you mean sth. like:
string _content = "1;2;3"

int _nPos = _content.IndexOf(";");

int i = 0;

while ( _nPos > 0 )

{

myArray[i++] = int.Parse(_content.Substring(0, _nPos ));

_content = _content.Substring(_nPos+1);

_nPos = _content.IndexOf(";");

}



Ruediger

Jeroen Ceuppens said:
Hi, i put some data in xml, i get the data out of the xml and put it
in
a
string
string data= "1;2;3;4"

So i need something that do the ; away and puts every number into the array
i:

1 goes to i[0]
2 goes to i[1]

and so on........

How can i do this?

Greetz
JC
 
Have a look at regular expressions.
The following is taken from the msdn on the Regex.Split method

Regex r = new Regex("(-)"); // Split on hyphens.
string[] s = r.Split("one-two-banana");

following the example, your code would become

Regex r = new Regex("(;)"); // Split on hyphens.
string[] s = r.Split("1;2;3;4");

Richard Lewis
 
In his case you don't even need the regular expressions
string[] items = "1;2;3;4".Split(';');
int[] values = new int[items.Length];
for( int i = 0; i < items.Length; i++ )
values = int.Parse(items);


Richard Lewis said:
Have a look at regular expressions.
The following is taken from the msdn on the Regex.Split method

Regex r = new Regex("(-)"); // Split on hyphens.
string[] s = r.Split("one-two-banana");

following the example, your code would become

Regex r = new Regex("(;)"); // Split on hyphens.
string[] s = r.Split("1;2;3;4");

Richard Lewis

Jeroen Ceuppens said:
Hi, i put some data in xml, i get the data out of the xml and put it in a
string
string data= "1;2;3;4"

So i need something that do the ; away and puts every number into the array
i:

1 goes to i[0]
2 goes to i[1]

and so on........

How can i do this?

Greetz
JC
 
Back
Top