Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

string.Split() not working

Posted on 2005-03-23
7
Medium Priority
?
382 Views
Last Modified: 2012-08-13
with the code below is not splitting the string correctly. instead of getting parsed like this:
strArray[1] = 1
strArray[2] = 2
strArray[3] = 3

i'd get:

strArray[1] = 1
strArray[2] = 2,3
strArray[3] =


Any Ideas? Code below:

string item = "1,2,3";
string delimStr = ",";
char[] delimiter = delimStr.ToCharArray();
string[] strArray = null;
            
for(int x = 0; x <= 2; x++)
  {
      strArray = item.Split(delimiter,x);
  }

      ItemsLabel.Text = strArray[0];
      QtyLabel.Text = strArray[1];
      PriceLabel.Text = strArray[2];
                                                                                    
                  }
0
Comment
Question by:vinny45
7 Comments
 

Author Comment

by:vinny45
ID: 13609494
this is good if you know the length of the array.

string item = "1,2,3";
string[] items = item.Split(',');
ItemsLabel.Text = items[0];
QtyLabel.Text = items[1];
PriceLabel.Text = items[2];

0
 
LVL 13

Accepted Solution

by:
dungla earned 2000 total points
ID: 13609570
instead of use

string delimStr = ",";
char[] delimiter = delimStr.ToCharArray();

Just use

char delimiter = ',';

===
or just do something like that

string item = "1,2,3";
string[] arrItems = item.Split(','); <-- this will return an array have 3 elements
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 13609581
string item = "1,2,3";
string[] items = item.Split(',');
int n = items.GetLength(0);
ItemsLabel.Text = (n >= 1 ? items[0] : "");
QtyLabel.Text = (n >= 2 ? items[1] : "");
PriceLabel.Text = (n >= 3 ? items[2] : "");
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:rajaloysious
ID: 13609648
you must use char[]. i was struck too for a while with this... before getting to this

string delimiterString = ",";
char [] delimiter = delimiterString.ToCharArray();
string AllDepts = "ABC,BBA,FGH,THY";
string [] dept_array=null;

dept_array = AllDepts.Split(delimiter);

for(int i=0; i<dept_array.Length; i++)
       MessageBox.Show( dept_array[i]);

Cheers..
0
 

Expert Comment

by:sudha_m_a
ID: 13613379
You could just use char[] as {','}  for delim character/characters

 String Str = "abc,ewew,ewew,ggdf,fdgd";
                  String[] items ;
                  char[] delim = {','};
                  items = Str.Split(delim);
                  for ( int i = 0; i< items.Length ; i++)
                  {
                        Console.WriteLine(items[i]);
                  }
0
 
LVL 2

Expert Comment

by:rmorreau
ID: 13614855
The problem is with the following lines of code

for(int x = 0; x <= 2; x++)
  {
     strArray = item.Split(delimiter,x);
  }

There is no need to loop through while calling the Split method.  Furthermore, the second argument to Split() indicates the maximum number of elements to return; which is 2 after the last iteration of your loop.  That's why your seeing the values 1 at strArray[0] and 2,3 at strArray[1].

Check here for further information on the String.Split method.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemstringclasssplittopic.asp

Best of luck,
Regards,
Ryan

0
 
LVL 1

Expert Comment

by:rashmitodkar
ID: 13619783
hay try this

    string item = "1,2,3";
    string delimStr = ",";
    char[] delimiter = delimStr.ToCharArray();
    string[] strArray = null;
         
    strArray = item.Split(delimiter);


     ItemsLabel.Text = strArray[0];
     QtyLabel.Text = strArray[1];
     PriceLabel.Text = strArray[2];

     actually u dont required for loop to use split function
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question