Click here to Skip to main content
15,904,024 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have searched for a way of reading the contents of a textarea into a two-dimensional array.

Say this is the textarea populated with content from a log:

<asp:TextBox ID="txtLog" Width="600px" Height="400px" TextMode="MultiLine" runat="server">
17/11/2010 09:34:46,codewriter31,email_Add_1,anyname@there.com
17/11/2010 09:34:46,codewriter31,email_Add_2,
17/11/2010 09:34:46,codewriter31,town_or_City,London
17/11/2010 09:35:22,gamer4,email_Add_1,petname@myhome.com
17/11/2010 09:35:22,gamer4,email_Add_2,
17/11/2010 09:35:22,gamer4,town_or_City,Pais
17/11/2010 09:39:08,tom_thumb,email_Add_1,someone@somewhere.com
17/11/2010 09:39:08,tom_thumb,email_Add_2,person@isp.com
17/11/2010 09:39:08,tom_thumb,town_or_City,New York
</asp:TextBox>


Each line has timestamp, user name, field name and optional value.

There are many code samples to read from a textfile but I am stuck with a textarea.

This code only gives me a one-dimensional array of lines:

// Define which character is separating fields
char[] splitter = { '\n' }; // '/n' is new line
string[] arrLine = Log.Split(splitter);


Does anyone have any pointers for me please?

E.g. array, ArrayList, List, Dictionary, hash table, DataTable?

How to read the textarea into it?

I am using C# ASP.NET 3.5.

Thank you
Posted
Updated 17-Nov-10 4:29am
v2

You're going to have to get the array of lines and parse each one for the details you want, adding those to a 2 dimensional array however your requirements specify. Probably the first dimension would be the record index and the 2nd would be the field index. Parsing the line would best be left up to a Regular Expression.
 
Share this answer
 
But what do you want to split each line into?

You could do something like:
string[][] lines = new string[arrLine.Length][];
for(int index = 0; index< arrLine.Length; index++)
{  
  lines[index] = string.split(arrLine[index], ",", 2);
}

This will split each line at the first comma.

Have a look here for more info:
http://dotnetperls.com/string-split[^]

Good luck!
 
Share this answer
 
v2
Comments
FayZee 17-Nov-10 11:31am    
What am I missing here? I have errors.

1 string Log = txtLog.Text;
2
3 // Define which character is separating fields
4 char[] splitter = { '\n' }; // '/n' is new line
5 string[] arrLine = Log.Split(splitter);
6
7 string[][] lines = new string[arrLine][];
8 for (int index = 0; index < arrLine.length; index++)
9 {
10 lines[index] = string.split(arrLine[index], ",", 2);
11 }


Line 7: Cannot implicitly convert type 'string[]' to 'int'
Line 8: 'System.Array' does not contain a definition for 'length' and no extension method 'length' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)
Line 10: 'string' does not contain a definition for 'split'
FayZee 17-Nov-10 11:33am    
<pre>
What am I missing here? I have errors.

1 string Log = txtLog.Text;
2
3 // Define which character is separating fields
4 char[] splitter = { '\n' }; // '/n' is new line
5 string[] arrLine = Log.Split(splitter);
6
7 string[][] lines = new string[arrLine][];
8 for (int index = 0; index < arrLine.length; index++)
9 {
10 lines[index] = string.split(arrLine[index], ",", 2);
11 }
</pre>
Line 7: Cannot implicitly convert type 'string[]' to 'int'
Line 8: 'System.Array' does not contain a definition for 'length' and no extension method 'length' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)
Line 10: 'string' does not contain a definition for 'split'
E.F. Nijboer 17-Nov-10 18:02pm    
Ah yes, the length is needed to define the array and not the array itself. I changed the answer and corrected it.
FayZee 18-Nov-10 4:34am    
Thank you for your help so far.
I'm still struggling with the line: lines[index] = string.split(arrLine[index], ",", 2);
The error is: 'string' does not contain a definition for 'split'.
I've got "using System;" at the top of the code.
FayZee 18-Nov-10 5:39am    
Success!

I have marked this as my accepted answer.

I changed the line: lines[index] = string.Split(arrLine[index], ",", 2);
To: lines[index] = arrLine[index].Split(',');

Then to access: TxtResult.Text = lines[2][3];
var text = txtLog.Text;
var lines = text.Split('\n');
var textList = new List<string[]>();

foreach(var line in lines)
{
  var textListItem = new List<string>();
  var items = line.split(',');
  foreach(var item in items)
  {
    textListItem.Add(item);
  }
  textList.Add(textListItem.ToArray());
}
var finalArray = textList.ToArray();
 
Share this answer
 
Comments
FayZee 18-Nov-10 4:52am    
Thank you, Jim, for your suggestion.
I have a similar problem with this code as with Answer 2:
'string' does not contain a definition for 'split' and no extension method 'split' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?).
Among other directives, I've got "using System;" and "using System.Collections.Generic;" at the top of the code.
FayZee 18-Nov-10 5:15am    
Okay, the line: var items = line.split(',');
should be: var items = line.Split(',');
with an upper case S for Split.

I have never worked with lists before, so how would I access row 2, column 3?
I would expect the result to be "London".
FayZee 18-Nov-10 5:39am    
Success!

To access: TxtResult.Text = finalArray[2][3].ToString();

I have marked this as my alternative solution.

Firstly, you have ssuggested a lot of var declarations. I don't know if this is usual?

Also, I do not have the experience to evaluate the relative merits of List<t> over String.Split.

Any guidance on either of these points please, anyone?
jim lahey 18-Nov-10 5:50am    
sorry about the lower case "split", unfortunately for me CodeProject doesn't have intellisense :)

var declarations are a constant point of discussion in my office. I like them personally because it's less typing and I don't have to import so many namespaces, others don't as they can't see at a glance what type they're dealing with. my counter to that argument is that you should understand what your code is doing anyway.

why do I use List<t> over arrays?

I use Lists instead because:

- You don't have to specify a length when you declare it
- Lists have an .Add() method
- Lists are dead easy to convert back to arrays should you need to
- Lists have useful methods like .Contains() .Find() and .FindAll()
FayZee 18-Nov-10 6:18am    
Cheers for the clarification.
Understood re. "you should understand what your code is doing anyway" but when the methods are unfamiliar ... :)

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