删除';字符串中的分隔符

本文关键字:分隔符 字符串 删除 | 更新日期: 2023-09-27 18:16:29

你好,我正在尝试使用字符串的拆分方法拆分字符串

 string toSplit = "hello how 'are u"
 string[] arr = toSplit.Split('''); // that code doesnt compile 
 for (int i=0 ; i < arr.Length ; i++)
      Console.write("arr[i]="+ arr[i]);

我的输出是:

arr[0] = hello 
arr[1]=how, // i get this output by removing the split ofc , it doesnt compile 
arr[2]='are 
and arr[3]=u

我想要的是从arr[2]中删除这个分隔符

提前感谢你的帮助。

删除';字符串中的分隔符

您只需执行

toSplit = toSplit.Replace("'", "");

在拆分之前

但我不太理解你的问题。您的标题表示要从字符串中删除"。

我也不确定你的代码是如何通过'分割来获得数组中的4个对象的,因为你的字符串中只有一个。

如果使用空格字符进行拆分,则数组看起来会是这样的。

所以这样做可以得到你想要的输出:

string toSplit = "hello how 'are u";
toSplit = toSplit.Replace("'", ""); 
string[] arr = toSplit.Split(' '); 
for (int i=0 ; i < arr.Length ; i++) 
    Console.Write("arr[i]="+ arr[i]); 
string toSplit = "hello how 'are u";
string[] arr = toSplit.Split('''');
var arr=arr.Split(' ');
 for (int i=0 ; i < arr.Length ; i++)
      Console.Write("arr[i]="+ arr[i]);

代码错误:

  1. 字符串声明行中没有分号
  2. 您没有转义'请使用转义符'
  3. 控制台。写入而不是写入