本文整理汇总了C#中System.String.Join方法的典型用法代码示例。如果您正苦于以下问题:C# String.Join方法的具体用法?C# String.Join怎么用?C# String.Join使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.String
的用法示例。
在下文中一共展示了String.Join方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
int maxPrime = 100;
List<int> primes = GetPrimes(maxPrime);
Console.WriteLine("Primes less than {0}:", maxPrime);
Console.WriteLine(" {0}", String.Join(" ", primes));
}
private static List<int> GetPrimes(int maxPrime)
{
Array values = Array.CreateInstance(typeof(int),
new int[] { maxPrime - 1}, new int[] { 2 });
// Use Sieve of Eratosthenes to determine prime numbers.
for (int ctr = values.GetLowerBound(0); ctr <= (int) Math.Ceiling(Math.Sqrt(values.GetUpperBound(0))); ctr++)
{
if ((int) values.GetValue(ctr) == 1) continue;
for (int multiplier = ctr; multiplier <= maxPrime / 2; multiplier++)
if (ctr * multiplier <= maxPrime)
values.SetValue(1, ctr * multiplier);
}
List<int> primes = new List<int>();
for (int ctr = values.GetLowerBound(0); ctr <= values.GetUpperBound(0); ctr++)
if ((int) values.GetValue(ctr) == 0)
primes.Add(ctr);
return primes;
}
}
输出:
Primes less than 100: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
示例2: Main
//引入命名空间
using System;
using System.Collections.Generic;
using System.Linq;
public class Example
{
public static void Main()
{
string output = String.Join(" ", GetAlphabet(true).Where( letter =>
letter.CompareTo("M") >= 0));
Console.WriteLine(output);
}
private static List<string> GetAlphabet(bool upper)
{
List<string> alphabet = new List<string>();
int charValue = upper ? 65 : 97;
for (int ctr = 0; ctr <= 25; ctr++)
alphabet.Add(Convert.ToChar(charValue + ctr).ToString());
return alphabet;
}
}
输出:
M N O P Q R S T U V W X Y Z
示例3: Main
//引入命名空间
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
int maxPrime = 100;
int[] primes = GetPrimes(maxPrime);
Console.WriteLine("Primes less than {0}:", maxPrime);
Console.WriteLine(" {0}", String.Join(" ", primes));
}
private static int[] GetPrimes(int maxPrime)
{
Array values = Array.CreateInstance(typeof(int),
new int[] { maxPrime - 1}, new int[] { 2 });
// Use Sieve of Eratosthenes to determine prime numbers.
for (int ctr = values.GetLowerBound(0); ctr <= (int) Math.Ceiling(Math.Sqrt(values.GetUpperBound(0))); ctr++)
{
if ((int) values.GetValue(ctr) == 1) continue;
for (int multiplier = ctr; multiplier <= maxPrime / 2; multiplier++)
if (ctr * multiplier <= maxPrime)
values.SetValue(1, ctr * multiplier);
}
List<int> primes = new List<int>();
for (int ctr = values.GetLowerBound(0); ctr <= values.GetUpperBound(0); ctr++)
if ((int) values.GetValue(ctr) == 0)
primes.Add(ctr);
return primes.ToArray();
}
}
输出:
Primes less than 100: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
示例4: if
object[] values = { null, "Cobb", 4189, 11434, .366 };
if (values[0] == null) values[0] = String.Empty;
Console.WriteLine(String.Join("|", values));
输出:
|Cobb|4189|11434|0.366
示例5: Main
//引入命名空间
using System;
public class JoinTest
{
public static void Main()
{
Console.WriteLine(MakeLine(0, 5, ", "));
Console.WriteLine(MakeLine(1, 6, " "));
Console.WriteLine(MakeLine(9, 9, ": "));
Console.WriteLine(MakeLine(4, 7, "< "));
}
private static string MakeLine(int initVal, int multVal, string sep)
{
string [] sArr = new string [10];
for (int i = initVal; i < initVal + 10; i++)
sArr[i - initVal] = String.Format("{0,-3}", i * multVal);
return String.Join(sep, sArr);
}
}
输出:
0 , 5 , 10 , 15 , 20 , 25 , 30 , 35 , 40 , 45 6 12 18 24 30 36 42 48 54 60 81 : 90 : 99 : 108: 117: 126: 135: 144: 153: 162 28 < 35 < 42 < 49 < 56 < 63 < 70 < 77 < 84 < 91
示例6:
String[] val = {"apple", "orange", "grape", "pear"};
String sep = ", ";
String result;
Console.WriteLine("sep = '{0}'", sep);
Console.WriteLine("val[] = {{'{0}' '{1}' '{2}' '{3}'}}", val[0], val[1], val[2], val[3]);
result = String.Join(sep, val, 1, 2);
Console.WriteLine("String.Join(sep, val, 1, 2) = '{0}'", result);
输出:
sep = ', ' val[] = {'apple' 'orange' 'grape' 'pear'} String.Join(sep, val, 1, 2) = 'orange, grape'
示例7: Animal
//引入命名空间
using System;
using System.Collections.Generic;
using System.Linq;
public class Animal
{
public string Kind;
public string Order;
public Animal(string kind, string order)
{
this.Kind = kind;
this.Order = order;
}
public override string ToString()
{
return this.Kind;
}
}
public class Example
{
public static void Main()
{
List<Animal> animals = new List<Animal>();
animals.Add(new Animal("Squirrel", "Rodent"));
animals.Add(new Animal("Gray Wolf", "Carnivora"));
animals.Add(new Animal("Capybara", "Rodent"));
string output = String.Join(" ", animals.Where( animal =>
(animal.Order == "Rodent")));
Console.WriteLine(output);
}
}
输出:
Squirrel Capybara
示例8: String.Join(String join, String value)
//引入命名空间
using System;
class MainClass
{
public static void Main()
{
string[] myStrings = {"To", "be", "or", "not", "to", "be"};
string myString9 = String.Join(".", myStrings);
Console.WriteLine("myString9 = " + myString9);
}
}