本文整理汇总了C#中System.String.Concat方法的典型用法代码示例。如果您正苦于以下问题:C# String.Concat方法的具体用法?C# String.Concat怎么用?C# String.Concat使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.String
的用法示例。
在下文中一共展示了String.Concat方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.Collections;
public class Example
{
public static void Main()
{
const int WORD_SIZE = 4;
// Define some 4-letter words to be scrambled.
string[] words = { "home", "food", "game", "rest" };
// Define two arrays equal to the number of letters in each word.
double[] keys = new double[WORD_SIZE];
string[] letters = new string[WORD_SIZE];
// Initialize the random number generator.
Random rnd = new Random();
// Scramble each word.
foreach (string word in words)
{
for (int ctr = 0; ctr < word.Length; ctr++)
{
// Populate the array of keys with random numbers.
keys[ctr] = rnd.NextDouble();
// Assign a letter to the array of letters.
letters[ctr] = word[ctr].ToString();
}
// Sort the array.
Array.Sort(keys, letters, 0, WORD_SIZE, Comparer.Default);
// Display the scrambled word.
string scrambledWord = String.Concat(letters[0], letters[1],
letters[2], letters[3]);
Console.WriteLine("{0} --> {1}", word, scrambledWord);
}
}
}
输出:
home --> mheo food --> oodf game --> aemg rest --> trse
示例2: Main
//引入命名空间
using System;
public class Example
{
public static void Main()
{
String s1 = "We went to a bookstore, ";
String s2 = "a movie, ";
String s3 = "and a restaurant.";
var s = String.Concat(s1, s2, s3);
Console.WriteLine(s);
}
}
输出:
We went to a bookstore, a movie, and a restaurant.
示例3: Main
//引入命名空间
using System;
public class ConcatTest {
public static void Main() {
// we want to simply quickly add this person's name together
string fName = "Simon";
string mName = "Jake";
string lName = "Harrows";
// because we want a name to appear with a space in between each name,
// put a space on the front of the middle, and last name, allowing for
// the fact that a space may already be there
mName = " " + mName.Trim();
lName = " " + lName.Trim();
// this line simply concatenates the two strings
Console.WriteLine("Welcome to this page, '{0}'!", string.Concat( string.Concat(fName, mName), lName ) );
}
}
输出:
Welcome to this page, 'Simon Jake Harrows'!
示例4: Main
//引入命名空间
using System;
class stringConcat5 {
public static void Main() {
int i = -123;
Object o = i;
Object[] objs = new Object[] {-123, -456, -789};
Console.WriteLine("Concatenate 1, 2, and 3 objects:");
Console.WriteLine("1) {0}", String.Concat(o));
Console.WriteLine("2) {0}", String.Concat(o, o));
Console.WriteLine("3) {0}", String.Concat(o, o, o));
Console.WriteLine("\nConcatenate 4 objects and a variable length parameter list:");
Console.WriteLine("4) {0}", String.Concat(o, o, o, o));
Console.WriteLine("5) {0}", String.Concat(o, o, o, o, o));
Console.WriteLine("\nConcatenate a 3-element object array:");
Console.WriteLine("6) {0}", String.Concat(objs));
}
}
输出:
Concatenate 1, 2, and 3 objects: 1) -123 2) -123-123 3) -123-123-123 Concatenate 4 objects and a variable length parameter list: 4) -123-123-123-123 5) -123-123-123-123-123 Concatenate a 3-element object array: 6) -123-456-789
示例5: Main
//引入命名空间
using System;
public class Example
{
public static void Main()
{
// Make an array of strings. Note that we have included spaces.
string [] s = { "hello ", "and ", "welcome ", "to ",
"this ", "demo! " };
// Put all the strings together.
Console.WriteLine(string.Concat(s));
// Sort the strings, and put them together.
Array.Sort(s);
Console.WriteLine(string.Concat(s));
}
}
输出:
hello and welcome to this demo! and demo! hello this to welcome
示例6: Main
//引入命名空间
using System;
public class ConcatTest {
public static void Main() {
// Create a group of objects.
Test1 t1 = new Test1();
Test2 t2 = new Test2();
int i = 16;
string s = "Demonstration";
// Place the objects in an array.
object [] o = { t1, i, t2, s };
// Concatenate the objects together as a string. To do this,
// the ToString method of each of the objects is called.
Console.WriteLine(string.Concat(o));
}
}
// Create two empty test classes.
class Test1 {
}
class Test2 {
}
输出:
Test116Test2Demonstration
示例7: Main
//引入命名空间
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
int maxPrime = 100;
IEnumerable<String> primeList = GetPrimes(maxPrime);
Console.WriteLine("Primes less than {0}:", maxPrime);
Console.WriteLine(" {0}", String.Concat(primeList));
}
private static IEnumerable<String> GetPrimes(int maxPrime)
{
Array values = Array.CreateInstance(typeof(int),
new int[] { maxPrime - 1}, new int[] { 2 });
// Use Sieve of Erathsthenes 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<String> primes = new List<String>();
for (int ctr = values.GetLowerBound(0); ctr <= values.GetUpperBound(0); ctr++)
if ((int) values.GetValue(ctr) == 0)
primes.Add(ctr.ToString() + " ");
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
示例8: Main
//引入命名空间
using System;
using System.Collections.Generic;
using System.Linq;
public class Example
{
public static void Main()
{
string output = String.Concat( 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;
}
}
输出:
MNOPQRSTUVWXYZ
示例9: 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.Concat(animals.Where( animal =>
(animal.Order == "Rodent")));
Console.WriteLine(output);
}
}
输出:
SquirrelCapybara
示例10: String.Concat(String value1, String value2)
//引入命名空间
using System;
class MainClass
{
public static void Main()
{
string myString4 = String.Concat("A, ", "B");
Console.WriteLine("String.Concat(\"A, \", \"B\") = "+ myString4);
string myString5 = String.Concat("A, ", "B, ", "and countrymen");
Console.WriteLine("String.Concat(\"A, \", \"B, \", " + "\"and countrymen\") = " + myString5);
}
}