4

I have a string array of a dynamic size.

For example:

string[] UserName_arr = new string[usercount + 1]; 
// here usercount would be int value considering it as 4 so the array size would be 5.

I need to add every UserName_arr values into a single string merging with just a special character < symbol.

When I use this code main_UserName = String.Join("<", UserName_arr);

I get the string as main_UserName =a1<a2<a3< I don't need the < in the end of my string

I checked out this link but was not able to reach anywhere

Community
  • 1
  • 1
TechBrkTru
  • 346
  • 1
  • 25
  • 6
    String.Join works as you want. I can only imagine that there is a null or empty string at the last position. Try `String.Join("<", UserName_arr.Where(x => !string.IsNullOrEmpty(x)))` – Sebastian Schumann Jun 11 '15 at 05:12
  • 1
    I get the result by the above code specified @ Vera rind – TechBrkTru Jun 11 '15 at 05:16
  • 1
    @Verarind post that as an answer. And TechBrkTru check this out as well: http://stackoverflow.com/questions/559415/concat-all-strings-inside-a-liststring-using-linq – aspiring Jun 11 '15 at 05:17
  • 1
    Why do you create the array with `[usercount + 1]`? Why one more? To me it looks that the last value will always be null and that will result in your result. Try the code of my last comment or init the array to `[usercount]`. – Sebastian Schumann Jun 11 '15 at 05:17
  • 1
    u mean just because I 'm adding usercount+1 I may be getting the null value right ,but even with the code that you specified do give me the result..@Verarind – TechBrkTru Jun 11 '15 at 05:20
  • Yes. Why do you need usercount+1 instead of usercount? – Sebastian Schumann Jun 11 '15 at 05:20
  • actually I thought for example if array size is 5 ,the last one should be null while declare hence I added it as usercount+1 ,because usercount would be 4 and +1 would be null – TechBrkTru Jun 11 '15 at 05:24
  • 1
    This is C#. You don't need a null value as last value of an array. Maybe you need a zero at the last position in a string (in C++, ...) but not in an array. Remove it if possible and you'll get the result you want with your code `String.Join("<", UserName_arr)`. – Sebastian Schumann Jun 11 '15 at 05:28
  • ya sure will try like that @Verarind – TechBrkTru Jun 11 '15 at 05:29
  • Don't forget to mark @ pwas as answer. Maybe @Rohit will also be okay, because there is a good explanation of the problem. – Sebastian Schumann Jun 11 '15 at 05:40

2 Answers2

5

Would this be what you trying to do?

UserName_arr.Aggregate((x,y) => x + "<" + y);

You can check out more on Aggregate here.

Or you can do TrimEnd in your code :

main_UserName = String.Join("<", UserName_arr);
main_UserName = main_UserName.TrimEnd('<');

String.Join example :

string[] dinosaurs = new string[] { "Aeolosaurus",
        "Deinonychus", "Jaxartosaurus", "Segnosaurus" };        
string joinedString = string.Join(", ", dinosaurs);
Console.WriteLine(joinedString);

Output :

Aeolosaurus, Deinonychus, Jaxartosaurus, Segnosaurus

See there is no , in the end.

See String.Join this example.

Edit :

Based on OP's comment and Vera rind's comments the problem OP faced was the wrong declaration of String array. It had one element more than required, which resulted in being a Null element in the end of the array. This array when used with String.Join due to the null last element resulted in an unwanted "<" at the end.

Either change your array declaration to :

string[] UserName_arr = new string[usercount]; 

Or check for null string in the Join condition :

String.Join("<", UserName_arr.Where(x => string.IsNullOrEmpty(x) == false))
Rohit Vipin Mathews
  • 11,629
  • 15
  • 57
  • 112
3

Like 'Vera rind' mentioned in comment, you can just omit the empty user names in your array:

main_UserName = String.Join(
                         "<", 
                         UserName_arr.Where(name => !string.IsNullOrWhiteSpace(name));

The problem is that last element in your array is null or empty - that is way last comma is added after that there is nothing.