c# 字符串删除指定字符串
时间: 2023-08-22 09:05:12 浏览: 46
可以使用 C# 中的 Replace 方法来删除指定字符串,例如:
```csharp
string str = "hello world";
string toRemove = "world";
string result = str.Replace(toRemove, "");
```
这样就可以将字符串中的 "world" 删除,得到结果 "hello"。如果要删除多个指定字符串,可以依次调用 Replace 方法。
相关问题
c#字符串中是否指定字符串
您可以使用C#中的Contains方法来判断一个字符串中是否包含另一个字符串。例如:
string str1 = "hello world";
string str2 = "world";
bool contains = str1.Contains(str2); // contains为true
如果您需要忽略大小写,可以使用StringComparison.OrdinalIgnoreCase参数:
bool containsIgnoreCase = str1.Contains(str2, StringComparison.OrdinalIgnoreCase); // containsIgnoreCase为true
c#如何删除字符串指定字符
可以使用以下代码删除字符串中的指定字符:
```csharp
string str = "hello, world!";
char charToRemove = ',';
string newStr = new string(str.Where(c => c != charToRemove).ToArray());
```
这里使用了 LINQ 的 Where 方法和 ToArray 方法,其中 Where 方法返回一个新的字符序列,其中不包含指定的字符,ToArray 方法将其转换为字符数组,然后使用 new string 方法创建一个新的字符串。