C# 去除string[]逗号转为string
时间: 2023-09-09 14:06:34 浏览: 40
如果你希望将一个 string[] 数组中的所有字符串中的逗号去除,并将结果以字符串形式返回,你可以使用以下代码:
```csharp
string[] arr = { "hello, world", "how, are, you" };
string result = string.Join(" ", arr.Select(s => s.Replace(",", "")));
```
这个代码会遍历数组中的所有字符串,将其中的逗号替换为空格,然后用 `string.Join` 方法将所有字符串拼接成一个字符串,中间用空格隔开。最终,`result` 变量的值将会是:
```
"hello world how are you"
```
相关问题
C# 去除string[]逗号
如果你想将一个 string[] 数组中的所有字符串中的逗号去除,你可以使用以下代码:
```csharp
string[] arr = { "hello, world", "how, are, you" };
for (int i = 0; i < arr.Length; i++)
{
arr[i] = arr[i].Replace(",", "");
}
```
这个代码会遍历数组中的所有字符串,将其中的逗号替换为空字符串。最终,`arr` 数组中的元素将会变成:
```
{ "hello world", "how are you" }
```
C# string[]转为string
可以使用 `string.Join` 方法将 `string[]` 转换为 `string`,例如:
```csharp
string[] array = new string[] { "hello", "world" };
string result = string.Join(",", array); // 使用逗号连接数组元素
Console.WriteLine(result); // 输出:hello,world
```
在上面的示例中,`string.Join` 方法将 `array` 数组中的元素使用逗号连接起来,并返回一个新的字符串。你也可以使用其他的分隔符,根据需要进行调整。