0

I have string similar to this

string text = "3;4;5;6;7;;3;4;5;6;7;;3;4;5;6;7;;3;4;5;6;7;;3;4;5;6;7;;";

I want to convert the above string into an int array like

[[3,4,5,6,7],[3,4,5,6,7], [3,4,5,6,7], [3,4,5,6,7], [3,4,5,6,7]] 

I want to do this so that I can access each number because I want to do some maths like (3+4)/2 and (5+6)/2 for each set of array.

Until now I am using a simple split method:

string[] columns = text.Split(';');

and printing it by using:

foreach (var item in columns)
{
    Console.WriteLine($"{item}");            
}

But this has not worked and I am getting one number after another each indicating separate item. I want [3,4,5,6,7] as one item and 5 items inside this particular item and in integer form. Can you help me with how to do this?

2 Answers 2

2

Try it like this:

var columns = text.Split(new[] { ";;" }, StringSplitOptions.RemoveEmptyEntries);            
foreach (var column in columns)
{
     var numbers = column.Split(';').Select(y => int.Parse(y));
}

If you want empty entries, you have to Change the StringSplitOptions to StringSplitOptions.None

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

2 Comments

important to note that you added the StringSplitOptions.RemoveEmptyEntries property (at the split function) .... maybe he wants to be able to create empty arrays for inputs like "1;2;;;;3;4" that should result in [[1,2],[],[3,4]]
No, I dont want to create empty array, infact I added two semicolons in the string(;;) to make it easier to divide the string in equal parts.
2
var output = text.Split(";;", StringSplitOptions.RemoveEmptyEntries)
    .Select(s => s.Split(";").Select(i => int.Parse(i)));

Split first on the ;;.

Then for each group, split again on ;.

Then parse each to int.


Update as per comment

I assume that empty entries need to be ignored, but if not, just remove the StringSplitOptions:

var output = text.Split(";;")
    .Select(s => s.Split(";").Select(i => int.Parse(i)));

Or if you need to be compatible with .NET Framework:

var output = text.Split(new [] { ";;" }, StringSplitOptions.RemoveEmptyEntries)
    .Select(s => s.Split(';').Select(i => int.Parse(i)));

Or to include empty entries:

var output = text.Split(new [] { ";;" }, StringSplitOptions.None)
    .Select(s => s.Split(';').Select(i => int.Parse(i)));

9 Comments

This won't work, because there is no string.Split(string) Method. See here learn.microsoft.com/de-de/dotnet/api/…
Same as for @SteveZ answer, important to note that you added the StringSplitOptions.RemoveEmptyEntries property (at the split function) .... maybe he wants to be able to create empty arrays for inputs like "1;2;;;;3;4" that should result in [[1,2],[],[3,4]]
@SteveZ There is in .NET Core: learn.microsoft.com/en-us/dotnet/api/…
I am using foreach loop to display items of outputs but getting this instead: System.Linq.Enumerable+WhereSelectArrayIterator`2[System.String,System.Int32]
@VedantGonnade Your question stated that you wanted integers; if your simply writing to the console then int.Parse is an unnecessary step. Regardless, to write to the console you would do foreach (var item in output) { Console.WriteLine(string.Join(",", item)); }
|

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.