10 Replies - 366 Views - Last Post: 17 June 2020 - 09:42 AM Rate Topic: -----

#1 capsyl   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 17-June 20

Prevent String.Join to print empty with int values

Posted 17 June 2020 - 08:10 AM

I tried the following code but end up with "CS1503" Argument 1: cannot convert from 'int' to 'string'.
Console.WriteLine(string.Join(",", numbers.Where(x => !string.IsNullOrEmpty(x))));

This was my initial code which worked fine despite int values:
Console.WriteLine(string.Join(", ", numbers));


The solution I tried to convert int to string with no luck:
Console.WriteLine(string.Join(",", numbers.Select(x => x.ToString()).ToArray().Where(x => !string.IsNullOrEmpty(x))));

I'm new to c# so any help/suggestions is much appreciated!

The title got a bit messed up. Don't see any place where to edit it?

Is This A Good Question/Topic? 0
  • +

Replies To: Prevent String.Join to print empty with int values

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15728
  • View blog
  • Posts: 63,014
  • Joined: 12-June 08

Re: Prevent String.Join to print empty with int values

Posted 17 June 2020 - 08:33 AM

What is "numbers"?
Was This Post Helpful? 0
  • +
  • -

#3 capsyl   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 17-June 20

Re: Prevent String.Join to print empty with int values

Posted 17 June 2020 - 08:34 AM

It's referencing to the following array:
int[] numbers = new int[10];
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15728
  • View blog
  • Posts: 63,014
  • Joined: 12-June 08

Re: Prevent String.Join to print empty with int values

Posted 17 June 2020 - 08:45 AM

I am to gather the int array has null or empty values in it?

(because an integer array typically would not have an empty space or null given the current definition)
Was This Post Helpful? 0
  • +
  • -

#5 capsyl   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 17-June 20

Re: Prevent String.Join to print empty with int values

Posted 17 June 2020 - 08:47 AM

This is the output:
1, 0, 0, 0, 0, 0, 0, 0, 0, 0

If there're empty (?) values, I guess the zero is empty or null? Or what is 0 in the array considered as?

I'm looking for an output like this:
1
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15728
  • View blog
  • Posts: 63,014
  • Joined: 12-June 08

Re: Prevent String.Join to print empty with int values

Posted 17 June 2020 - 08:56 AM

A normal integer variable is initialized to 0. In a normal integer won't accept, on face, an empty string or null.

You can test this out by trying:
int foo = "";
int bar = null;


If you want to pick out a specific value from the array, then you can use the 'WHERE' clause in LINQ to do so. like 'WHERE X != 0'.
Was This Post Helpful? 0
  • +
  • -

#7 capsyl   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 17-June 20

Re: Prevent String.Join to print empty with int values

Posted 17 June 2020 - 09:10 AM

Thanks but that requires a bit more code doesn't it?

Initially I had the following which felt overkill, but on the other hand I'm new so I don't know what's best/bad practise:
foreach (var item in numbers)
{
    if (item != 0)
        Console.Write(item + ", ");
}


Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15728
  • View blog
  • Posts: 63,014
  • Joined: 12-June 08

Re: Prevent String.Join to print empty with int values

Posted 17 June 2020 - 09:21 AM

I mean not really if you are using LINQ.

Example if I break down the steps for removing a specific character from an array.

//create the array
            char[] myChars = new char[3];

            myChars[0] = 'a';
            myChars[1] = 'z';
            myChars[2] = 'c';

//Use linq to return a list. 
            List<char> stringifiedExclude = (from x in myChars
                                      where x != 'z'
                                     select x).ToList();
//the string.join function
            string postJoin = string.Join(",", stringifiedExclude);
//the fruits of the labor

            Console.WriteLine(postJoin);


a,c


(Again, the steps are broken down and this all can be combined to a slightly less readable jumble and less broken out calls)
Was This Post Helpful? 1
  • +
  • -

#9 capsyl   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 17-June 20

Re: Prevent String.Join to print empty with int values

Posted 17 June 2020 - 09:28 AM

Thanks! That seem pretty slick! Will play around a bit with that!
Was This Post Helpful? 0
  • +
  • -

#10 capsyl   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 17-June 20

Re: Prevent String.Join to print empty with int values

Posted 17 June 2020 - 09:40 AM

I found the following code to work out as well:
Console.WriteLine(string.Join(",", numbers.Where(x => x != 0)));

Was This Post Helpful? 0
  • +
  • -

#11 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15728
  • View blog
  • Posts: 63,014
  • Joined: 12-June 08

Re: Prevent String.Join to print empty with int values

Posted 17 June 2020 - 09:42 AM

Yep, LINQ and Lambdas are two routes to skin the cat.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1