Csharp/CSharp Tutorial/String/String Concat

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Append strings using Concat()

<source lang="csharp">using System;

class MainClass {

 public static void Main() { 

   string result = String.Concat("This ", "is ", "a ", 
                                 "test ","."); 

   Console.WriteLine("result: " + result); 

 } 

}</source>

result: This is a test .

Concatenate string with integers

<source lang="csharp">using System; class MainClass {

 public static void Main() {  
 
   string result = String.Concat("The value is " + 19); 
   Console.WriteLine("result: " + result);  

 }  

}</source>

result: The value is 19

Concatenate string with non-strings

<source lang="csharp">using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Text;

public class MainClass {

   public static void Main()
   {
       int number = 10;
       string msg = "age is " + number + ".";
       msg += " great?";
       Console.WriteLine("msg: {0}", msg);
       String s1 = 10 + 5 + ": Two plus three is " + 2 + 3;
       String s2 = 10 + 5 + ": Two plus three is " + (2 + 3);
       Console.WriteLine("s1: {0}", s1);
       Console.WriteLine("s2: {0}", s2);    }

}</source>

msg: age is 10. great?
s1: 15: Two plus three is 23
s2: 15: Two plus three is 5

Mix string and integer in string cancatenation

<source lang="csharp">using System; class MainClass {

 public static void Main() {  
   string result = String.Concat("hello ", 88, " ", 20.0, " ", 
                          false, " ",  23.45M);  
   Console.WriteLine("result: " + result);  
 }

}</source>

result: hello 88 20 False 23.45

String concatenation

<source lang="csharp">using System; using System.Collections.Generic; using System.Text;

 class Program
 {
   static void Main(string[] args)
   {
     string s1 = "A ";
     string s2 = "B";
     string s3 = string.Concat(s1, s2);
     Console.WriteLine(s3);
   }
 }</source>