繁体   English   中英

从字符串中删除定界符

[英]removing ' delimiters from a string

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

 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]删除此定界符

在此先感谢您的帮助。

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. Console.Write而不是

你可以做

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]); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM