Saturday, 6 July 2024

Join Method in String Manipulation

 

Join Method Concatenates an array of strings into a single string with a specified separator using String.Join() Method.

 

string result;

string[] fruits = { "Apple", "Banana", "Orange", "Grapes", "Jamakaya", "Bathayi" };

char[] ABCD = { 'A', 'B', 'C', 'D' };

List<string> fruits1 = new List<string>() { "Apple", "Banana", "Orange", "Grapes" };

 

 

//separator and string array as parameters

result = String.Join(", ", fruits);

Output: Apple, Banana, Orange, Grapes, Jamakaya, Bathayi

 

//separator and Char array as parameters

result = String.Join(", ", ABCD);

Output: A, B, C, D

 

// separator and list of strings as parameter

result = String.Join(", ", fruits1);

Output:Apple, Banana, Orange, Grapes

 

//separator and string array , start index and count as parameters

result = String.Join(", ", fruits, 2, 3);

Output: "Orange, Grapes, Jamakaya"

 

 

// separator and string params

result = String.Join(", ", "Apple", "Banana", "Orange", "Grapes", "Jamakaya", "Bathayi");

Output: "Apple, Banana, Orange, Grapes, Jamakaya, Bathayi"

 

 

// separator and int params

result = String.Join(", ", 10, 20, 30, 40, 50, 60);

Output: "10, 20, 30, 40, 50, 60"

 

// separator and mixed num params

result = String.Join(", ", 10.2, 20, 30.02, 40, 50, 60.5);

Output: "10.2, 20, 30.02, 40, 50, 60.5"

 

// separator and char params

result = String.Join(", ", 'A', 'B', 'C', 'D');

Output:"A, B, C, D"

 

// seperator and Mixed datatypes

 result = String.Join(", ", "English",'A', 'B', 'C', 'D',10,20,54,36);

Output : "English, A, B, C, D, 10, 20, 54, 36"

No comments:

Post a Comment

Thanks for the contribution, our team will check and reply back if response required.