Phương thức
Replace[string oldValue, string newValue]
1 trả về một chuỗi mới bằng cách thay thế từng ký tự/chuỗi con phù hợp trong chuỗi bằng ký tự/chuỗi con mớiusing System;
namespace CsharpString {
class Test {
public static void Main[string [] args] {
string str = "Programming is fun";
// replaces "Programming" with "C#"
string result = str.Replace["Programming", "C#"];
Console.WriteLine[result];
Console.ReadLine[];
}
}
}
// Output: C# is fun
Thay thế [] Cú pháp
Cú pháp của phương thức chuỗi
Replace[string oldValue, string newValue]
1 làReplace[string oldValue, string newValue]
Ở đây,
Replace[string oldValue, string newValue]
1 là một phương thức của lớp Replace[string oldValue, string newValue]
1Thay thế [] Tham số
Phương thức
Replace[string oldValue, string newValue]
1 nhận các tham số sau- oldValue - chuỗi con mà chúng tôi muốn thay thế
- newValue - chuỗi con mới sẽ thay thế chuỗi con cũ
Thay thế [] Giá trị trả về
- Phương thức
1 trả về một chuỗi mới bằng cách thay thế từng ký tự/chuỗi con phù hợp trong chuỗi bằng ký tự/chuỗi con mớiReplace[string oldValue, string newValue]
ví dụ 1. Chuỗi C# Thay thế [] Ký tự
using System;
namespace CsharpString {
class Test {
public static void Main[string [] args] {
string str = "Bat";
Console.WriteLine["Old value: " + str];
string result;
// replaces 'B' with 'C''
result = str.Replace['B', 'C'];
Console.WriteLine["New value: " + result];
Console.ReadLine[];
}
}
}
đầu ra
Old value: Bat New value: Cat
Trong ví dụ trên, phương thức
Replace[string oldValue, string newValue]
1 thay thế ký tự Replace[string oldValue, string newValue]
5 bằng Replace[string oldValue, string newValue]
6ví dụ 2. Thay thế chuỗi C# [] Chuỗi con
using System;
namespace CsharpString {
class Test {
public static void Main[string [] args] {
string str = "C# Programming";
string result;
// replaces "C#" with "Java"
result = str.Replace["C#", "Java"];
Console.WriteLine["New Value1: " + result];
// returns initial string
result = str.Replace["Go", "C++"];
Console.WriteLine["New Value2: " + result];
Console.ReadLine[];
}
}
}
đầu ra
________số 8
7 - thay chuỗi conReplace[string oldValue, string newValue]
8 bằng chuỗiReplace[string oldValue, string newValue]
9Replace[string oldValue, string newValue]
0 - trả về chuỗi ban đầu vì không có chuỗi conusing System; namespace CsharpString { class Test { public static void Main[string [] args] { string str = "Bat"; Console.WriteLine["Old value: " + str]; string result;
// replaces 'B' with 'C'' result = str.Replace['B', 'C'];
1using System; namespace CsharpString { class Test { public static void Main[string [] args] { string str = "Bat"; Console.WriteLine["Old value: " + str]; string result;
// replaces 'B' with 'C'' result = str.Replace['B', 'C'];
Console.WriteLine["New value: " + result]; Console.ReadLine[]; } } }
ví dụ 3. Chuỗi thay thế []
Replace[string oldValue, string newValue]
4đầu ra
Replace[string oldValue, string newValue]
5Trong ví dụ trên, chúng tôi đã thực hiện nhiều lần thay thế trong chuỗi ban đầu
Các cuộc gọi phương thức được thực hiện từ trái sang phải. Cho nên,
- đầu tiên,
2 sẽ được thay thế bằngusing System; namespace CsharpString { class Test { public static void Main[string [] args] { string str = "Bat"; Console.WriteLine["Old value: " + str]; string result;
// replaces 'B' with 'C'' result = str.Replace['B', 'C'];
Console.WriteLine["New value: " + result]; Console.ReadLine[]; } } }
3using System; namespace CsharpString { class Test { public static void Main[string [] args] { string str = "Bat"; Console.WriteLine["Old value: " + str]; string result;
// replaces 'B' with 'C'' result = str.Replace['B', 'C'];
- sau đó,
3 sẽ được thay thế bằngusing System; namespace CsharpString { class Test { public static void Main[string [] args] { string str = "Bat"; Console.WriteLine["Old value: " + str]; string result;
// replaces 'B' with 'C'' result = str.Replace['B', 'C'];
Console.WriteLine["New value: " + result]; Console.ReadLine[]; } } }
5using System; namespace CsharpString { class Test { public static void Main[string [] args] { string str = "Bat"; Console.WriteLine["Old value: " + str]; string result;
// replaces 'B' with 'C'' result = str.Replace['B', 'C'];
Console.WriteLine["New value: " + result]; Console.ReadLine[]; } } }
Do đó, chúng tôi nhận được
using System;
namespace CsharpString {
class Test {
public static void Main[string [] args] {
string str = "Bat";
Console.WriteLine["Old value: " + str];
string result;
// replaces 'B' with 'C''
result = str.Replace['B', 'C'];
Console.WriteLine["New value: " + result];
Console.ReadLine[];
}
}
}
5 là đầu raGhi chú. Phương thức
Replace[string oldValue, string newValue]
1 không sửa đổi giá trị của phiên bản hiện tại. Thay vào đó, nó trả về một chuỗi mới bằng cách thay thế sự xuất hiện của các giá trị cũ bằng các giá trị mới