Answered by:
how to split line of text string to 3 parts

Question
-
i have one textbox with values like this: "wael fikri; mikke fahmeey; shaji philip; Ai Jan;"
what i want is to sepret this string to 3 parts first part will take the first name the words befour ";" semicoln second part will take second name 3rd part will take all names come after
i have 3 varible as string
A1,A2,A3
i want code to give value to this varible like this:
A1="wael fikri";
A2="mikke fahmeey";
A3="shaji philip; Ai Jan;";
any sample code please
Thanks for any help :) my blog is: http://www.waelk.comMonday, January 16, 2012 1:03 PM
Answers
-
Drop the 2 from the split i.e. use string[] names = line.Split(delimiter);
The 2 says that is the maximum number of substrings, you want them all.
You might investigate the String.Split method in the help - some useful overloads in there.
Regards David R
---------------------------------------------------------------
Every program eventually becomes rococo, and then rubble. - Alan Perlis
The only valid measurement of code quality: WTFs/minute.- Marked as answer by Bob Shen Monday, January 30, 2012 2:55 AM
Sunday, January 22, 2012 4:58 PM -
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { //i have one textbox with values like this: "wael fikri; mikke fahmeey; shaji philip; Ai Jan;" String textbox = "wael fikri; mikke fahmeey; shaji philip; Ai Jan;"; //i have 3 varible as string String a1 = "A1"; String b1 = "B1"; String c1 = "C1"; String[] split = textbox.Split(new String[] { ";" }, StringSplitOptions.RemoveEmptyEntries); String outputA1 = String.Format("{0}={1}", a1, split[0]); String outputB1 = String.Format("{0}={1}", b1, split[1]); String outputC1 = String.Format("{0}={1}", c1, split[2] + ";" + split[3]); } } }
John Grove, Senior Software Engineer http://www.digitizedschematic.com/Sunday, January 22, 2012 5:16 PM -
string gfg = "wael fikri; mikke fahmeey; shaji philip; Ai Jan;Peter Koueik";
string[] df = gfg.Split(';');
string rdpart3 = "";
for (int i = 0; i < df.Length; i++)
{
if (i == 0)
{
string stpart1 = df[i].ToString();
Lbll.Text = stpart1;
}
else if (i == 1)
{
string ndpart2 = df[i].ToString();
lbll2.Text = ndpart2;
}
else
{
rdpart3 += df[i].ToString()+" ";
Lbll3.Text =rdpart3;
}
}
This worked as you demanded i have tried it...
just copy and paste the code!!It will print in the first label the first value
in the second the second value
in the third the rest of the values!
Peter Koueik- Proposed as answer by Peter Koueik Thursday, January 26, 2012 5:37 AM
- Marked as answer by Bob Shen Monday, January 30, 2012 2:55 AM
Thursday, January 26, 2012 5:37 AM
All replies
-
read the link below
http://www.dotnetperls.com/split
http://msdn.microsoft.com/en-us/library/b873y76a.aspx
http://msdn.microsoft.com/en-us/library/tabh47cf.aspx
Use the seperator as ;
Hope it will help you
Want to add MVP with my name.Monday, January 16, 2012 1:15 PM -
Here is a code:
char[] delimiter = {';'}; string line = "wael fikri; mikke fahmeey; shaji philip; Ai Jan;"; string[] names = line.Split(delimiter, 2); string A1 = names[0]; string A2 = names[1].Substring(0, names[1].IndexOf(";")); string A3 = names[1].Substring(names[1].IndexOf(";") + 1);
Noam B.
Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...Monday, January 16, 2012 1:17 PM -
string gfg = "wael fikri; mikke fahmeey; shaji philip; Ai Jan;Peter Koueik";
string[] df = gfg.Split(';');
string 3rdpart= "";
for (int i = 0; i < df.Length; i++)
{
if (i == 0)
{
string 1stpart = df[i].ToString();
Lbll.Text = 1stpart;
}
else if (i == 1)
{
string 2ndpart = df[i].ToString();
lbll2.Text = 2ndpart;
}
else
{
3rdpart += df[i].ToString();
Lbll3.Text =3rdpart;
}
}
Peter Koueik- Edited by Peter Koueik Monday, January 16, 2012 1:27 PM
- Proposed as answer by Peter Koueik Tuesday, January 17, 2012 8:59 AM
- Unproposed as answer by wael_e Sunday, January 22, 2012 1:08 PM
Monday, January 16, 2012 1:26 PM -
thanks for you all friends.
i tryed the 2 codes but both give me the result as like this first one A1 empty, second one A2 have one name first name , 3rd one A3 have the full names after first one.
like this:
A1=
A2=wael fikri
A3= mikke fahmeey shaji philip Ai Jan
Thanks for any help :) my blog is: http://www.waelk.comSunday, January 22, 2012 1:12 PM -
Drop the 2 from the split i.e. use string[] names = line.Split(delimiter);
The 2 says that is the maximum number of substrings, you want them all.
You might investigate the String.Split method in the help - some useful overloads in there.
Regards David R
---------------------------------------------------------------
Every program eventually becomes rococo, and then rubble. - Alan Perlis
The only valid measurement of code quality: WTFs/minute.- Marked as answer by Bob Shen Monday, January 30, 2012 2:55 AM
Sunday, January 22, 2012 4:58 PM -
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { //i have one textbox with values like this: "wael fikri; mikke fahmeey; shaji philip; Ai Jan;" String textbox = "wael fikri; mikke fahmeey; shaji philip; Ai Jan;"; //i have 3 varible as string String a1 = "A1"; String b1 = "B1"; String c1 = "C1"; String[] split = textbox.Split(new String[] { ";" }, StringSplitOptions.RemoveEmptyEntries); String outputA1 = String.Format("{0}={1}", a1, split[0]); String outputB1 = String.Format("{0}={1}", b1, split[1]); String outputC1 = String.Format("{0}={1}", c1, split[2] + ";" + split[3]); } } }
John Grove, Senior Software Engineer http://www.digitizedschematic.com/Sunday, January 22, 2012 5:16 PM -
string gfg = "wael fikri; mikke fahmeey; shaji philip; Ai Jan;Peter Koueik";
string[] df = gfg.Split(';');
string rdpart3 = "";
for (int i = 0; i < df.Length; i++)
{
if (i == 0)
{
string stpart1 = df[i].ToString();
Lbll.Text = stpart1;
}
else if (i == 1)
{
string ndpart2 = df[i].ToString();
lbll2.Text = ndpart2;
}
else
{
rdpart3 += df[i].ToString()+" ";
Lbll3.Text =rdpart3;
}
}
This worked as you demanded i have tried it...
just copy and paste the code!!It will print in the first label the first value
in the second the second value
in the third the rest of the values!
Peter Koueik- Proposed as answer by Peter Koueik Thursday, January 26, 2012 5:37 AM
- Marked as answer by Bob Shen Monday, January 30, 2012 2:55 AM
Thursday, January 26, 2012 5:37 AM