for (int i = 1; i < st.Length; i+=2)
{
Console.WriteLine(st[i]);
}
What if ' start from first index?
use reg expression instead
var pattern = @"\'(.*?)\'";
var query = "<ID Name='abc' EID='1' Age = '40l' Deptid = '9'/>";
var matches = Regex.Matches(query, pattern);
foreach (Match m in matches)
{
Response.Write(m.Groups[1].Value+"<br>");
}
Please Mark as Answer if find helpful -- Nasser -- Skype: maleknasser1
var pattern = @"\'(.*?)\'";
var query = "<ID Name='abc' EID='1' Age = '40l' Deptid = '9'/>";
var matches = Regex.Matches(query, pattern);
int indexes = matches.Count;
string[] myResult = new string[indexes];
int i = 0;
foreach (Match m in matches)
{
myResult[i] = m.Groups[1].Value;
i++;
}
Please Mark as Answer if find helpful -- Nasser -- Skype: maleknasser1
Marked as answer by prince2485 on May 08, 2012 01:58 PM
prince2485
Member
289 Points
257 Posts
how to split a string,c#/asp.net?
May 08, 2012 07:29 AM|LINK
guys,
i have a string <ID Name='abc' EID='1' Age = '40l' Deptid = '9'/>
i need the data inside single quotes(' ') in separate string.
tx in adv,
Usman Amir
Participant
790 Points
138 Posts
Re: how to split a string,c#/asp.net?
May 08, 2012 07:39 AM|LINK
use
now you "st" string array have your require string , now you can traverse array using for loop as
for (int i = 1; i < st.Length; i+=2) { Console.WriteLine(st[i]); }Nasser Malik
Star
10980 Points
1691 Posts
Re: how to split a string,c#/asp.net?
May 08, 2012 08:14 AM|LINK
What if ' start from first index?
use reg expression instead
var pattern = @"\'(.*?)\'"; var query = "<ID Name='abc' EID='1' Age = '40l' Deptid = '9'/>"; var matches = Regex.Matches(query, pattern); foreach (Match m in matches) { Response.Write(m.Groups[1].Value+"<br>"); }Skype: maleknasser1
prince2485
Member
289 Points
257 Posts
Re: how to split a string,c#/asp.net?
May 08, 2012 08:28 AM|LINK
Actual string/xml,
<ID Name='abc' EID='1' Age = '40l' Deptid = '9'>
</ID>
Nasser Malik
Star
10980 Points
1691 Posts
Re: how to split a string,c#/asp.net?
May 08, 2012 08:39 AM|LINK
doesn't matter ..
regular expression works
did you try ?
Skype: maleknasser1
prince2485
Member
289 Points
257 Posts
Re: how to split a string,c#/asp.net?
May 08, 2012 09:23 AM|LINK
your code works nicely,
but nw rather than displaying using response.write, i want it on string array, but i not getting in it .
it return weird o/p.
can you advice...tx in advance
sriramabi
Contributor
4351 Points
1277 Posts
Re: how to split a string,c#/asp.net?
May 08, 2012 09:36 AM|LINK
hai
pls see this
http://www.dotnetperls.com/split
thank u
Usman Amir
Participant
790 Points
138 Posts
Re: how to split a string,c#/asp.net?
May 08, 2012 09:38 AM|LINK
with reference to nasser post jst do following
var pattern = @"\'(.*?)\'"; var query = "<ID Name='abc' EID='1' Age = '40l' Deptid = '9'/>"; var matche = Regex.Matches(query, pattern); string[] values = matche.Cast<Match>().Select(x => (x.Value)).ToArray();now string array "values" having all values that you want to achieve
Gavy1313
Member
276 Points
115 Posts
Re: how to split a string,c#/asp.net?
May 08, 2012 09:42 AM|LINK
stringbuilder sbhint = new stringbuilder();
string input;
input = sbhint.ToString();
parts1 = input.Split(new string[] { "' '" }, StringSplitOptions.None);
int k;
for (k = 0; k < parts1.Length-1; k++)
{
you code// ViewState["parts"] += (parts1[k] + "<br>");
}
Nasser Malik
Star
10980 Points
1691 Posts
Re: how to split a string,c#/asp.net?
May 08, 2012 09:44 AM|LINK
var pattern = @"\'(.*?)\'"; var query = "<ID Name='abc' EID='1' Age = '40l' Deptid = '9'/>"; var matches = Regex.Matches(query, pattern); int indexes = matches.Count; string[] myResult = new string[indexes]; int i = 0; foreach (Match m in matches) { myResult[i] = m.Groups[1].Value; i++; }Skype: maleknasser1