Thao tác cơ bản trên chuỗi bằng C#
PHP Code:
string str = " Cac thao tac tren chuoi trong C# ";int l = str.Length;Console.WriteLine("\nDo dai cua chuoi = " + l);
- CONCAT(str1, str2): Nối chuỗi str2 sau chuỗi str1.
PHP Code:
string s1 = " Chao mung cac ban den voi ";
string s2 = "Dien dan CCI Group ";
Console.WriteLine("\nChuoi 1: \"" + s1 + "\"");
Console.WriteLine("\nChuoi 2: \"" + s2 + "\"");
str = string.Concat(s1, s2);
Console.WriteLine("\nChuoi sau khi noi la: \"" + str + "\"");
string s2 = "Dien dan CCI Group ";
Console.WriteLine("\nChuoi 1: \"" + s1 + "\"");
Console.WriteLine("\nChuoi 2: \"" + s2 + "\"");
str = string.Concat(s1, s2);
Console.WriteLine("\nChuoi sau khi noi la: \"" + str + "\"");
- COMPARE(str1, str2): So sánh chuỗi str1 với chuỗi str2. Kiểu trả về là -1, 0, 1. (So sánh từng ký tự trong bảng mã ASCII).
· Nếu trả về 1: Chuỗi str1 lớn hơn chuỗi str2.
· Nếu trả về 0: 2 chuỗi bằng nhau.
· Nếu trả về -1: Chuỗi str1 nhỏ hơn chuỗi str2.
PHP Code:
string s1 = "Chao mung cac ban den voi";
string s2 = "Dien dan CCI Group ";
Console.WriteLine("\nChuoi 1: \"" + s1 + "\"");
Console.WriteLine("\nChuoi 2: \"" + s2 + "\"");
int n= string.Compare(s1, s2);
if (n > 0)
Console.WriteLine("\nChuoi s1 lon chuoi s2");
else if (n < 0)
Console.WriteLine("\nChuoi s1 nho chuoi s2");
else
Console.WriteLine("\nChuoi s1 bang chuoi s2");
string s2 = "Dien dan CCI Group ";
Console.WriteLine("\nChuoi 1: \"" + s1 + "\"");
Console.WriteLine("\nChuoi 2: \"" + s2 + "\"");
int n= string.Compare(s1, s2);
if (n > 0)
Console.WriteLine("\nChuoi s1 lon chuoi s2");
else if (n < 0)
Console.WriteLine("\nChuoi s1 nho chuoi s2");
else
Console.WriteLine("\nChuoi s1 bang chuoi s2");
- COPY(str): Copy chuỗi str đến chuỗi khác.
PHP Code:
string s1 = "Chao mung cac ban den voi";
Console.WriteLine("\nChuoi 1: \"" + s1 + "\"");
string str = string.Copy(s1);
Console.WriteLine("\nChuoi can copy: "+ str);
Console.WriteLine("\nChuoi 1: \"" + s1 + "\"");
string str = string.Copy(s1);
Console.WriteLine("\nChuoi can copy: "+ str);
- INSERT(n, str): Thêm chuỗi str sau n ký tự của chuỗi cần chèn.
PHP Code:
string s1 = "Welcome CCI Group";
string s2 = "to ";
Console.WriteLine("\nChuoi 1: \"" + s1 + "\"");
Console.WriteLine("\nChuoi 2: \"" + s2 + "\"");
string str = s1.Insert(8, s2);
Console.WriteLine("\nChuoi sau khi chen: " + str);
string s2 = "to ";
Console.WriteLine("\nChuoi 1: \"" + s1 + "\"");
Console.WriteLine("\nChuoi 2: \"" + s2 + "\"");
string str = s1.Insert(8, s2);
Console.WriteLine("\nChuoi sau khi chen: " + str);
- REPLACE(str1, str2): Thay thế chuỗi str1 bằng chuỗi str2.
PHP Code:
string s1 = "Chao mung cac ban den voi";
string s2 = "Dien dan CCI Group ";
Console.WriteLine("\nChuoi 1: \"" + s1 + "\"");
Console.WriteLine("\nChuoi 2: \"" + s2 + "\"");
s2 = s2.Replace(s2, s1);
Console.WriteLine("\nChuoi 2 sau khi thay the: " + s2);
string s2 = "Dien dan CCI Group ";
Console.WriteLine("\nChuoi 1: \"" + s1 + "\"");
Console.WriteLine("\nChuoi 2: \"" + s2 + "\"");
s2 = s2.Replace(s2, s1);
Console.WriteLine("\nChuoi 2 sau khi thay the: " + s2);
- TRIM(): Xóa khoảng trắng thừa 2 bên chuỗi.
PHP Code:
string str = " Cac thao tac tren chuoi trong C# ";
Console.WriteLine("\nChuoi ban dau: \"" + str + "\"");
str = str.Trim();
Console.WriteLine("\nChuoi sau xoa khoang trang 2 ben: \"" + str + "\"");
Console.WriteLine("\nChuoi ban dau: \"" + str + "\"");
str = str.Trim();
Console.WriteLine("\nChuoi sau xoa khoang trang 2 ben: \"" + str + "\"");
- str.ToUpper & str.ToLower: IN HOA & chữ thường chuỗi str.
PHP Code:
string s1 = "Chao mung cac ban den voi";
string s2 = "Dien dan CCI Group ";
Console.WriteLine("\nChuoi 1: \"" + s1 + "\"");
Console.WriteLine("\nChuoi 2: \"" + s2 + "\"");
string Upper = s1.ToUpper();
string Lower = s2.ToLower();
Console.WriteLine("\nChuoi 1 - HOA: " + Upper);
Console.WriteLine("\nChuoi 2 - Thuong: " + Lower);
string s2 = "Dien dan CCI Group ";
Console.WriteLine("\nChuoi 1: \"" + s1 + "\"");
Console.WriteLine("\nChuoi 2: \"" + s2 + "\"");
string Upper = s1.ToUpper();
string Lower = s2.ToLower();
Console.WriteLine("\nChuoi 1 - HOA: " + Upper);
Console.WriteLine("\nChuoi 2 - Thuong: " + Lower);
Post a Comment