| | |
extracting code from string
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2008
Posts: 63
Reputation:
Solved Threads: 0
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.
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.
Like this:
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.
c# Syntax (Toggle Plain Text)
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
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
Make love, no war. Cave ab homine unius libri.
Danny
c# Syntax (Toggle Plain Text)
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]
Last edited by sknake; Aug 4th, 2009 at 9:31 am.
![]() |
Similar Threads
- plz help for writing strstr code without using <string.h> (C++)
- convert ascii code to string (C#)
- Run code from string? (C#)
- Why is strlen from string in asm code? (C++)
- extracting sustrings from a string and store it in an array (ASP.NET)
Other Threads in the C# Forum
- Previous Thread: Ontology Reasoner Plugin
- Next Thread: Excel slow/crashing when I generate a graph using C#
| Thread Tools | Search this Thread |
.net access ado.net algorithm array barchart bitmap box broadcast buttons c# check checkbox client color combobox control conversion csharp custom customactiondata database datagrid datagridview dataset date/time datetime datetimepicker degrees development dll draganddrop drawing encryption enum event excel file filename files form format forms function gdi+ gis gtk httpwebrequest image index input install java label list listbox mandelbrot math mouseclick mysql operator outlook2003 path photoshop picturebox pixelinversion pixelminversion post programming radians regex remote remoting richtextbox serialization server sleep socket sql statistics stream string table text textbox thread time timer tutorial update usercontrol validation visualstudio webbrowser webcam wia windows winforms wpf xml






