943,786 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 394
  • C# RSS
Aug 4th, 2009
0

extracting code from string

Expand Post »
hey there. thanks for all the help i have recieved from the people on this site!

i have a new problem, one i could solve if i was using VB but im using C# and i dont really know what to do.

i have data....


return1.MV TE244654.MV b43tgwg.sp
01.62 00.83 100.76
01.62 00.83 100.77
01.60 00.83 100.78
--.-- 00.83 100.79
--.-- 00.82 100.80
01.63 00.83 100.81
01.63 00.83 100.82
--.-- 00.83 100.83
--.-- 00.83 100.84
01.62 00.83 100.85
01.59 00.83 100.86
--.-- 00.83 100.88
--.-- 00.83 100.89
01.61 00.84 100.90
01.60 00.84 100.91

basically i need to organise this data into an array, but its more difficult that it seems.

like the array needs to be like this

values[x,y]
values[0,0] = return1.MV
values[0,1] = 01.62
values[0,2] = 01.62
values[1,0] = TE244654.MV
values[1,1] = 00.83

etc etc.

however each row is one big string
eg. " 01.62 00.83 100.26" so i need to separate each part to put into the array.

what would be the code i would use to make this happen in C#. as i said i know what i would need to do, and if i as using VB i could use left right and mid. if you could help that would be amazing.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
666kennedy is offline Offline
63 posts
since Mar 2008
Aug 4th, 2009
0

Re: extracting code from string

You could use the method SubString here like Mystr.SubString(charpos,lenghttoextract);
You can let this work like left,mid,right if you want.
Last edited by ddanbe; Aug 4th, 2009 at 7:25 am.
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,738 posts
since Oct 2008
Aug 4th, 2009
0

Re: extracting code from string

how do you set that up to use in the code. i have looked on msdn but its not completely apparent!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
666kennedy is offline Offline
63 posts
since Mar 2008
Aug 4th, 2009
0

Re: extracting code from string

Like this:
c# Syntax (Toggle Plain Text)
  1. string Mystr = "01.62 00.83 100.76";
  2. string Leftstr = Mystr.Substring(0, 5); // Leftstr is equal to 01.62
  3. string Midstr = Mystr.Substring(6, 5); // Midstr00.83
  4. string Rightstr = Mystr.Substring(Mystr.Length - 5, 5); // Rightstr 00.76
First integer of substring is the zero based char position in the string. The second integer is the length in characters of the string you want to extract.
Last edited by ddanbe; Aug 4th, 2009 at 9:17 am.
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,738 posts
since Oct 2008
Aug 4th, 2009
0

Re: extracting code from string

c# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace daniweb
  5. {
  6. class MultiArray
  7. {
  8. private static string[] GetTestData()
  9. {
  10. List<string> result = new List<string>();
  11. result.Add("return1.MV TE244654.MV b43tgwg.sp");
  12. result.Add("01.62 00.83 100.76");
  13. result.Add("01.62 00.83 100.77");
  14. result.Add("01.60 00.83 100.78");
  15. result.Add("--.-- 00.83 100.79");
  16. result.Add("--.-- 00.82 100.80");
  17. result.Add("01.63 00.83 100.81");
  18. result.Add("01.63 00.83 100.82");
  19. result.Add("--.-- 00.83 100.83");
  20. result.Add("--.-- 00.83 100.84");
  21. result.Add("01.62 00.83 100.85");
  22. result.Add("01.59 00.83 100.86");
  23. result.Add("--.-- 00.83 100.88");
  24. result.Add("--.-- 00.83 100.89");
  25. result.Add("01.61 00.84 100.90");
  26. result.Add("01.60 00.84 100.91");
  27. return result.ToArray();
  28. }
  29. internal static void DoWork()
  30. {
  31. string[] testData = GetTestData();
  32. int xLen = testData[0].Split(new char[] { ' ' }, StringSplitOptions.None).Length;
  33. int yLen = testData.Length;
  34. string[,] result = new string[xLen, yLen];
  35.  
  36. for (int iY = 0; iY < testData.Length; iY++)
  37. {
  38. string[] fields = testData[iY].Split(new char[] { ' ' }, StringSplitOptions.None);
  39. if (fields.Length != xLen)
  40. throw new Exception("You have a varying number of columns....");
  41.  
  42. for (int iX = 0; iX < fields.Length; iX++)
  43. {
  44. result[iX, iY] = fields[iX];
  45. }
  46. }
  47. System.Diagnostics.Debugger.Break();
  48. }
  49. }
  50. }

[edit]Oops, I didn't see Danny already answered it. His answer is probably closer to how you originally went about it, and will probably be easier to implement[/edit]
Last edited by sknake; Aug 4th, 2009 at 9:31 am.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Aug 4th, 2009
0

Re: extracting code from string

Don't worry Scott your posts are always appreciated and always provide new insights!
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,738 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Ontology Reasoner Plugin
Next Thread in C# Forum Timeline: Excel slow/crashing when I generate a graph using C#





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC