How to add character character in the middle of a string, but not at the beginning or end of a string in C #

I have a dynamic array string array.

For instance:

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 each UserName_arr value to a single line combining only a special character . <

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

I get the line as main_UserName =a1<a2<a3< I do not need at the end of my line <

I checked this link but could not reach anywhere

+4
source share
2 answers

Will this be what you are trying to do?

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

You can learn more about Aggregate here .

Or you can do TrimEndin 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

, ,.

. String.Join .

:

OP , OP String. , , , Null. String.Join - "<" .

:

string[] UserName_arr = new string[usercount]; 

Join:

String.Join("<", UserName_arr.Where(x => string.IsNullOrEmpty(x) == false))
+5

"Vera rind", , :

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

, , - , , .

+3

All Articles