extracting code from string

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 63
Reputation: 666kennedy is an unknown quantity at this point 
Solved Threads: 0
666kennedy 666kennedy is offline Offline
Junior Poster in Training

extracting code from string

 
0
  #1
Aug 4th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,955
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 282
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: extracting code from string

 
0
  #2
Aug 4th, 2009
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.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 63
Reputation: 666kennedy is an unknown quantity at this point 
Solved Threads: 0
666kennedy 666kennedy is offline Offline
Junior Poster in Training

Re: extracting code from string

 
0
  #3
Aug 4th, 2009
how do you set that up to use in the code. i have looked on msdn but its not completely apparent!
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,955
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 282
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: extracting code from string

 
0
  #4
Aug 4th, 2009
Like this:
  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.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,224
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 574
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: extracting code from string

 
0
  #5
Aug 4th, 2009
  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.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,955
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 282
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: extracting code from string

 
0
  #6
Aug 4th, 2009
Don't worry Scott your posts are always appreciated and always provide new insights!
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC