GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — C# String - Join() Method
Selected Reading
C# String - Join() MethodThe C# String Join() method is used to concatenate the element of an array or collection into a single string, with a specified delimiter separating each element or member. It can be overloaded based on the argument of the join method. SyntaxFollowing is the syntax of the C# string Join() method − string String.Join(string separator, IEnumerable<string> values); string String.Join(string separator, string[] values); ParametersThis method accepts the following parameters −
Return ValueThis method returns a string that contains elements of values parameter delimited by the separator character. Example 1: Joining an Array of StringFollowing is a basic example of the Join() method to join the array of strings into a single string −
using System;
class Program {
static void Main() {
string[] words = { "This", "is", "tutorialspoint" };
string result = String.Join(", ", words);
Console.WriteLine(result);
}
}
OutputFollowing is the output − This, is, tutorialspoint Example 2: Joining With Custom DelimiterLet's look at another example. Here, we use the Join() method to convert a character array into a single string − using System;
class Program {
static void Main() {
char[] letter = { 'A', 'B', 'C' };
string result = String.Join("|", letter);
Console.WriteLine(result);
}
}
OutputFollowing is the output − A|B|C Example 3: Use Join Method with StartIndex and CountHere, in this example, we use the Join() method with startIndex and count. It will only concatenate the string from the starting index to the specified number of elements − using System;
class Program {
static void Main() {
String[] val = {
"This",
"is",
"green",
"apple",
"you",
"want to",
"buy"
};
String sep = ", ";
String result;
Console.WriteLine("val[] = {{'{0}' '{1}' '{2}' '{3}'}}", val[0], val[1], val[2], val[3]);
result = String.Join(sep, val, 1, 4);
Console.WriteLine("String.Join(sep, val, 1, 4) = '{0}'", result);
}
}
OutputFollowing is the output − val[] = {'This' 'is' 'green' 'apple'}
String.Join(sep, val, 1, 4) = 'is, green, apple, you'
Example 4: Joining with Null or Empty Values:The example below uses the Join() method to join the string array into a single string. The join method treats using System;
class Program {
static void Main(){
// array with null value
string[] items = { "A", null, "C", "D" };
string result = String.Join(", ", items);
Console.WriteLine(result);
}
}
OutputFollowing is the output − A, , C, D csharp_strings.htm Advertisements
|
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |