1

I have a string which has many line and space.

For example:

string lines = "my name is omar\nliving in whateverf\ng i j";

I need to split it into a 2D array in which every word is in an index and each row represents a line.

Which means the 2D array should be more like this:

my      name  is omar
living  in    whatever
g       i     j

Is it possible?

I have tried splitting it first to lines and then spiting it to words, but I need id in a 2d array

string [] l = lines.split('\n');

for(int i = 0; i < l.length; i++)
{
     string [] oneLine= l[i].split(' ');
     //and put the rest of the code here
}
1
  • i edited it, hope i made my point more clear :D Commented Mar 31, 2019 at 5:14

2 Answers 2

2

Here is how you can have an array or arrays (string[][]):

 var results = z.Split(Environment.NewLine.ToArray()).Select(a => a.Split(' ')).ToArray();

If you still want to convert it to 2D array (string[,]) you can do it as described here.

Sign up to request clarification or add additional context in comments.

Comments

0
   l=string.split('\n')
   for i in range(0,len(l)):
        l[i]=l[i].split(" ")
    print(l) 

Split the string on basis of \n and then do again on each basis of word

2 Comments

i know this way , i need a way to save the whole in put string in a 2D array which has a word in an index, is it possible?
l is a 2-d array, where you can access a word l[0][1] would give you a certain word

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.