简体   繁体   English

发送不需要的字符到数组末尾

[英]Send unwanted Char's to end of array

Let's say we have a string 假设我们有一个字符串

String data = "<span> 0397]);}}:;)]</span>";

Then we send that into an char array 然后我们将其发送到一个char数组中

Char[] charData = data.ToCharArray();

How could I go trough this data and replace all unwanted data to the end of the array but move all data left one from the current char that has been moved to make room at the end for the unwanted data. 我该如何处理这些数据,并将所有不需要的数据替换到数组的末尾,但是将所有数据从当前char移到剩下的一个,以便为多余的数据留出空间。

Char[] sendToEnd = { ';', ')', '}', ']' };

I have tried a loop but it causes an infinite loop when it loops over the already swapped data. 我尝试了一个循环,但是当循环遍历已交换的数据时会导致无限循环。

In the end the string data should be 最后,字符串数据应为

"<span> 0397</span>]);}}:;)]"
var str = new String(data.Where(c => !sendToEnd.Contains(c))
                         .Concat(data.Where(c => sendToEnd.Contains(c)))
                         .ToArray());

Also declaring sendToEnd as 还声明sendToEnd

var sendToEnd = new HashSet<char>(new char[] { ';', ')', '}', ']' });

can give a better performance 可以提供更好的表现

与其将字符最初移动到末尾,不如将它们移动到另一个数组并将这些数组连接起来以形成最终的字符串。

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

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