extracting code from string
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.
666kennedy
Junior Poster in Training
63 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
You could use the method SubString here like Mystr.SubString(charpos,lenghttoextract);
You can let this work like left,mid,right if you want.
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
how do you set that up to use in the code. i have looked on msdn but its not completely apparent!
666kennedy
Junior Poster in Training
63 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
Like this:
string Mystr = "01.62 00.83 100.76";
string Leftstr = Mystr.Substring(0, 5); // Leftstr is equal to 01.62
string Midstr = Mystr.Substring(6, 5); // Midstr00.83
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.
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
using System;
using System.Collections.Generic;
namespace daniweb
{
class MultiArray
{
private static string[] GetTestData()
{
List<string> result = new List<string>();
result.Add("return1.MV TE244654.MV b43tgwg.sp");
result.Add("01.62 00.83 100.76");
result.Add("01.62 00.83 100.77");
result.Add("01.60 00.83 100.78");
result.Add("--.-- 00.83 100.79");
result.Add("--.-- 00.82 100.80");
result.Add("01.63 00.83 100.81");
result.Add("01.63 00.83 100.82");
result.Add("--.-- 00.83 100.83");
result.Add("--.-- 00.83 100.84");
result.Add("01.62 00.83 100.85");
result.Add("01.59 00.83 100.86");
result.Add("--.-- 00.83 100.88");
result.Add("--.-- 00.83 100.89");
result.Add("01.61 00.84 100.90");
result.Add("01.60 00.84 100.91");
return result.ToArray();
}
internal static void DoWork()
{
string[] testData = GetTestData();
int xLen = testData[0].Split(new char[] { ' ' }, StringSplitOptions.None).Length;
int yLen = testData.Length;
string[,] result = new string[xLen, yLen];
for (int iY = 0; iY < testData.Length; iY++)
{
string[] fields = testData[iY].Split(new char[] { ' ' }, StringSplitOptions.None);
if (fields.Length != xLen)
throw new Exception("You have a varying number of columns....");
for (int iX = 0; iX < fields.Length; iX++)
{
result[iX, iY] = fields[iX];
}
}
System.Diagnostics.Debugger.Break();
}
}
}
[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]
sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
Don't worry Scott your posts are always appreciated and always provide new insights!:)
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661