Click here to Skip to main content
15,901,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having the following string


C#
<Polygon>
          <Vertex x="9352.7606" y="8250.6001" z="505.3871" />
          <Vertex x="9352.7573" y="8250.6001" z="505.3844" />
          </Polygon>



I want to get the string as
C#
9352.7606    8250.6001     505.3871    
9352.7573    8250.6001     505.3844




How to split as shown above and store in a new string
Posted
Updated 28-Mar-14 18:48pm
v3
Comments
[no name] 28-Mar-14 11:25am    
Write a regular expression. Just out of curiosity, why do you post and repost the same questions over and over?
KUMAR619 28-Mar-14 11:40am    
Not working properly can any one get me a code

First, add this using namespace; statement:
C#
using System.Text.RegularExpressions;

Then, try this:
C#
string str = "your string here";
MatchCollection matches = Regex.Matches(str, @"\d+(\.\d+)?");
List<string> numbers = new List<string>();
foreach (Match m in matches)
{
    numbers.Add(m.Value);
}

If you don't want a list of strings, but a list of doubles, try this:
C#
string str = "your string here";
MatchCollection matches = Regex.Matches(str, @"\d+(\.\d+)?");
List<double> numbers = new List<double>();
foreach (Match m in matches)
{
    numbers.Add(Double.Parse(m.Value, new System.Globalization.NumberFormatInfo() { NumberDecimalSeparator = "." }));
}

My regular expression \d+(\.\d+)? looks for numbers with and without decimal point. If you only want to match numbers with a decimal point, remove the parentheses and the question mark: \d+\.\d+
 
Share this answer
 
v3
Comments
OriginalGriff 28-Mar-14 12:43pm    
Thanks for that!
I was going to write up the same thing, almost identical, and you saved me a job...then I thought "why not use Linq?"
Because you can't that's why!
Then I thought a little more, and it's simple to convert...so (indirectly) thanks to you I have knocked up a quick tip that lets you use Linq with Regexes:
http://www.codeproject.com/Tips/751437/Using-Linq-with-Regular-Expression-results
Have a +5 for making my life a lot simpler in future!
Thomas Daniels 28-Mar-14 12:50pm    
I've read your Tip, but it is not necessary to use your .AsEnumerable method on a MatchCollection or a GroupCollection in a foreach loop: foreach (Match m in matches) works fine; no need to call .AsEnumerable() on your matches object.
OriginalGriff 28-Mar-14 13:03pm    
I realized that and removed it shortly after pressing "publish" :O
I'd left it there after a copy'n'paste from VS which was a touch over enthusiastic... :doh:
(It was the prototype tester for the extension methods before I checked the Linq versions)
Thomas Daniels 28-Mar-14 13:03pm    
I see you removed the unnecessary statement, but I found something else: you said that MatchCollection doesn't implement IEnumerable, but it does implement IEnumerable, but not IEnumerable<T>. I think that needs to be fixed. Also, have a 5 for your tip for finding an easy way to use LINQ with regexes!
OriginalGriff 28-Mar-14 13:08pm    
Bugger! Griff, you idiot, if it didn't implement IEnumerable, you couldn't use foreach with it... :O
Thanks! Fixed.
 
Share this answer
 
Use a regular expression:
\d+\.\d+
Should do it...
 
Share this answer
 
Comments
KUMAR619 28-Mar-14 11:41am    
Sir I tried but its not working for me

Please give me a sample code
OriginalGriff 28-Mar-14 12:00pm    
ProgramFOX has a code solution that should work.
OriginalGriff 28-Mar-14 12:40pm    
Hah! Another solution: you can use regexes with Linq a lot more simply than I thought!
Using Linq with Regular Expression results[^]
KUMAR619 29-Mar-14 0:54am    
Is there any way to split numbers and store them in a list for each line

List<string> lstString=new List<string>();

Now split lines and split each numbers by "\t"

So that in
lstSting[1]="9352.7606 8250.6001 505.3871"
lstSting[2]="9352.7573 8250.6001 505.3844"

Then I can append using StringBuilder.

First I need to split numbers of each line and store in corresponding list

Please help me to to solve this
OriginalGriff 29-Mar-14 5:34am    
Yes - split the input text on '\n' and feed each line separately into the regex.
string.Split will do the first, and the second is just a simple loop, or a Linq statement.
This looks like XML, so an XML centric solution might be appropriate. That way it is impedance matched to the problem space.
(I'm not used to doing this on my new Android tablet... so no code from me this time.)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900