- การใช้โอเปอเรเตอร์ + และ +=
- การใช้ string interpolation
- การใช้ StringBuilder
- การใช้ string
- การใช้ LINQ
1.การใช้โอเปอเรเตอร์ + และ +=
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string userName = "Jack";
string dateString = DateTime.Today.ToShortDateString();
// Use the + and += operators for one-time concatenations.
string str = "Hello " + userName + ". Today is " + dateString + ".";
Console.WriteLine(str);
str += " How are you today?";
Console.WriteLine(str);
}
}
}
จะได้
Hello Jack. Today is 20/1/2562. Hello Jack. Today is 20/1/2562. How are you today?
2. การใช้ string interpolation
How to: Concatenate Multiple Strings (C# Guide)
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string userName = "Jack";
string date = DateTime.Today.ToShortDateString();
// Use string interpolation to concatenate strings.
string str = $"Hello {userName}. Today is {date}.";
Console.WriteLine(str);
str = $"{str} How are you today?";
Console.WriteLine(str);
}
}
}
วาง $ ไว้หน้า “” และวางตัวแปรไว้ภายในวงเล็บปีกกาได้เลย
3.การใช้ StringBuilder
3.1 Append() และ AppendFormat()
using System;
using System.Text;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
// Create a StringBuilder
StringBuilder sb = new StringBuilder();
// Append four characters to the end of the StringBuilder.
sb.Append(new char[] { 'A', 'B', 'C', ' ' });
// Append strings to the end of the StringBuilder.
sb.Append("DEF ");
// Append a format string to the end of the StringBuilder.
string name = "Jack";
sb.AppendFormat("Hello {0}", name);
// Display the number of characters in the StringBuilder and its string.
Console.WriteLine("Length={0} {1}", sb.Length, sb.ToString());
// Insert a string at the beginning of the StringBuilder.
sb.Insert(0, "Alphabet: ");
Console.WriteLine("Length={0} {1}", sb.Length, sb.ToString());
// Replace all lowercase k's with uppercase K's.
sb.Replace("Hello", "Hi");
Console.WriteLine("Length={0} {1}", sb.Length, sb.ToString());
}
}
}
จะได้
Length=18 ABC DEF Hello Jack Length=28 Alphabet: ABC DEF Hello Jack Length=25 Alphabet: ABC DEF Hi Jack
3.2 Append() และ AppendLine()
เมธอด Append() จะเพิ่ม string เข้ามาเฉยๆ
ส่วนเมธอด AppendLine() จะเพิ่ม string พร้อมกับขึ้นบรรทัดใหม่ให้ด้วย
แต่ถ้าจะใช้เมธอด Append แล้วขึ้นบรรทัดใหม่ ก็ต้องใส่ “\n” เอง
using System;
using System.Text;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder();
sb.Append("Hello ");
sb.AppendLine("Jack");
Console.WriteLine("> {0}", sb.ToString());
sb.AppendLine("Second line");
Console.WriteLine("> {0}", sb.ToString());
sb.AppendLine("Third line");
Console.WriteLine("> {0}", sb.ToString());
sb.Append("Fourth line\n");
Console.WriteLine("> {0}", sb.ToString());
sb.Append("Fifth line\n");
Console.WriteLine("> {0}", sb.ToString());
}
}
}
จะได้
> Hello Jack > Hello Jack Second line > Hello Jack Second line Third line > Hello Jack Second line Third line Fourth line > Hello Jack Second line Third line Fourth line Fifth line
4.การใช้ string
string.Concat() และ string.Join()
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string[] words = { "The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog." };
var unreadablePhrase = string.Concat(words);
Console.WriteLine(unreadablePhrase);
var readablePhrase = string.Join(" ", words);
Console.WriteLine(readablePhrase);
var other = string.Join("-", words);
Console.WriteLine(other);
}
}
}
จะได้
Thequickbrownfoxjumpsoverthelazydog. The quick brown fox jumps over the lazy dog. The-quick-brown-fox-jumps-over-the-lazy-dog.
string.Concat() จะเอา string มาต่อกันดื้อๆเลย
ส่วน string.Join() สามารถกำหนด string ที่จะมาคั่นระหว่างแต่ละ string ที่เอามาต่อกันได้
string.Format
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
decimal pricePerOunce = 17.36m;
string s = string.Format("The current price is {0} per ounce.", pricePerOunce);
Console.WriteLine(s);
// Result: The current price is 17.36 per ounce.
}
}
}
5.การใช้ LINQ
Language Integrated Query (LINQ)
using System;
using System.Linq;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string[] words = { "The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog." };
var phrase1 = words.Aggregate((partialPhrase, word) => $"{partialPhrase}{word}");
Console.WriteLine(phrase1);
var phrase2 = words.Aggregate((partialPhrase, word) => $"{partialPhrase} {word}");
Console.WriteLine(phrase2);
var phrase3 = words.Aggregate((partialPhrase, word) => $"{partialPhrase}-{word}");
Console.WriteLine(phrase3);
}
}
}
จะได้ผลลัพธ์เหมือนข้างบน