2

I am new to the C # language and would like to know how I do to cut or manipulate data from an array that is grouped in a single line

the data is like this

enter image description here .

the command line to make the data appear

IList<IList<Object>> values = response.Values;

            if (values != null && values.Count > 0)
            {
                foreach (var row in values)
                {        
                    Console.WriteLine(row[0]);
             String str = row[0].ToString().Split(Environment.NewLine,row[0]);
                }
              }

even converting the object to string I'm having an error in this conversion

EDIT

I managed to get him to remove whatever I want using the split I think this is already a start

                    try
                    {
                    String[] str = row[0].ToString().Split(null);
                    Console.WriteLine(str[0]);
                    Console.WriteLine($"{row[0]}");
                    catch (Exception)
                    {

                    }

i used a try to prevent errors

2
  • 4
    Can you add more code. Like how is row defined, and how is row[0] filled? Commented May 3, 2020 at 0:26
  • 1
    IList<Object> is rarely useful. Show us what response.Values contains from your debugger, not from a screenshot. Commented May 3, 2020 at 1:33

4 Answers 4

2

As mentioned above, you can use Split method to split the string. For example, if string value is "Europe, Asia, Africa":

string stringToSplit = "Europe, Asia, Africa";
string[] arrayOfString = stringToSplit.Split(',');

This overload (version) of Split method takes one argument and that is the character used to split the string. Result will be Europe, Asia and Africa. However, Asia and Africa will have one space character (' ') in the beginning because there is a space before those words in stringToSplit. So additionally, you need to trim elements of arrayOfString by adding

.Select(s => s.Trim()).ToArray()

The result would look like this:

string stringToSplit = "Europe, Asia, Africa";
string[] arrayOfString = stringToSplit.Split(',').Select(s => s.Trim()).ToArray();

stringToSplit is string you want to split using ',' character. Split(',') is the method that splits the string using ',' character and the result of that method is an array of string-s. Select(s => s.Trim()) takes each element of that array and trims it (removes all empty spaces from the beginning and from the end) and the result of it is a list of strings. s is just an alias for element, you can name it whatever you want. ToArray is a method that converts, in this case the list returned by Select(s => s.Trim()), to an array. Finally, the result stored in arrayOfString is an array containing trimmed strings.

Having in mind the result you've posted, in your example

IList<IList<Object>> values = response.Values;

if (values != null && values.Count > 0)
{
    foreach (var row in values)
    {        
        Console.WriteLine(row[0]);
    }
}

values is a list of lists. In other words, each element of values (named row in the example) is a list. After you check if values exists (values != null) and if has elements (values.Count > 0), you go through each element (row) of values. Those elements are lists (IList<Object>) and you write the first element of each row by using row[0].

Definition IList<IList<Object>> values says that row is of type IList<Object>, which means that every element of row is of type Object. Method WriteLine has an overload (version) that accepts an argument of type Object as it is in this case. That argument is then implicitly (in the background) converted to type string.

Looking at the output, there's no doubt that those element of type Object are actually strings, otherwise it will write name of the type, or some other data not useful, or not readable.

There are two possibilities.

Either list values contains one element (one list of objects, row) and the first element of row is a string (it is definitely of type string) that contains all those names separated by Environment.NewLine, or list values contains multiple elements (lists of objects, row-s) and the first element of each row is a string that contains single name.

If you know that you accept one string and need to separate it, than ok, and it would be the first possibility. In that case you could try

row[0].ToString().Split('\n')

However, it seems like the other case, where there are multiple lists of objects and the first one is name. In that case you don't need to split anything.

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

2 Comments

i tried many things and get output this System.String[] for all lines
You have probably passed entire array to WriteLine instead of its elements and as output you have got name of the array type. Inside existing foreach, instead of WriteLine and other code inside that foreach, create a variable string[] stringArray and store the split to that variable. Then create another one foreach and write every element of array: foreach (var str in stringArray) { Console.WriteLine(str); }.
2

First, convert the object to a string with .ToString() and then use the .Split() function together with Environment.NewLine as the token for the splitting.

Comments

1

Alternatively, you can unwind a list of lists such as IList<List<object>> with the .SelectMany() command from System.Linq.

Example program below:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            IList<IList<object>> values = new List<IList<object>>
            {
                new object[] { "Kassadin" },
                new object[] { "Wukong" },
                new object[] { "Lucian-" },
                new object[] { "Akali" },
                new object[] { "Brand-" },
                new object[] { "Kassadin" },
                new object[] { "Kassadin" },
            };

            var list = values.SelectMany((row) => row).Select((item) => item.ToString());
            foreach (var item in list)
            {
                Console.WriteLine(item);
            }
        }
    }
}

with the expected output:

Kassadin
Wukong
Lucian-
Akali
Brand-
Kassadin
Kassadin

Comments

1

It looks like one string, but with newlines inside.

If this is the case then you can split using newline character as separator.

For instance : var individualLines = row[0].Split(new[] { Environment.NewLine });

2 Comments

I'm having an error String str = row[0].ToString().Split(Environment.NewLine,row[0]); i can convert string to char and obj to char
@FelipeCruz I changed the syntax a bit, but now I realized something : the returned result of string.Split() is an array, not a single string. So you should use var strs = ..., or string[] strs = ...., not string str = .....

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.