c#split string

时间:2017-02-03 01:18:15

标签: c# android arrays xamarin

我有这个代码可以拆分特定的字符串。

str = "\n\nThis\nString\nis\nsplit\ninto\narray";
string[] delimiterChar = { "\n" };

var splitArray = str.Split(delimiterChar);

split数组是简单的字符串数组,但为什么不能使用以下方法删除前2项:

splitArray[0].remove();
splitArray[0].remove();

3 个答案:

答案 0 :(得分:2)

如果需要删除空条目,则只需在split方法中指定它。无需手动删除它们。

<html>
  <head>
   </head>
  <body>
    <div class="animation-container">
       <div class="car-right">
           <img class="car-right-image"src="http://www.freeiconspng.com/uploads/car-png-10.png" alt="car">
       </div>
       <div class="car-left">
           <img class="car-left-image"src="http://www.freeiconspng.com/uploads/car-png-10.png" alt="car">
       </div>
       
    </div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
    <div>Content here</div>
   </body>
  
  
  </html>

如果您想要专门删除某些特定元素,那么您可以将数组(不可变)转换为列表(可变),并按照以下方式处理:

var splitArray = str.Split(delimiterChar, StringSplitOptions.RemoveEmptyEntries);

答案 1 :(得分:0)

这可能是可能获得理想结果的解决方案之一。

string str = "\n\nThis\nString\nis\nsplit\ninto\narray";
str = str.TrimStart('\n');
var splitArray = str.Split('\n');

答案 2 :(得分:0)

  

是的,但有时字符串在前3个索引中包含随机字符串..   所以我想通过拆分将这个简单的字符串拆分成数组并删除。

     

split数组是简单的字符串数组,但为什么不能删除前2个项

将其拆分,然后使用Linq跳过任意数量的项目:

// I am skipping 3 for example
var splitArray = str.Split(delimiterChar).Skip(3).ToList();

确保将其添加到using语句列表中:

using System.Linq;