?
Solved

string.Split() not working

Posted on 2005-03-23
7
Medium Priority
?
374 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
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!

 
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

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!

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

764 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