如何通过与另一个字符串进行比较来删除字符串中的字符 [英] How can I delete character from a string by compare with another string
本文介绍了如何通过与另一个字符串进行比较来删除字符串中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果
string S1 =1,2,3,4;
string S2 =2,3,5,9,10,;
预期结果是
字符串结果= 1 ,4,5,9,10
我尝试过:
请帮助
if
string S1 = "1,2,3,4";
string S2= "2,3,5,9,10,";
expection result is
string result = 1,4,5,9,10
What I have tried:
please help
here 2 and 3 is common.I want to remove this to result.
推荐答案
John建议的替代方法是使用Linq方法:
An alternative to John's suggestion is to use Linq methods:
string S1 = "1,2,3,4";
string S2 = "2,3,5,9,10,";
string[] p1 = S1.Split(',');
string[] p2 = S2.Split(',');
string result = string.Join(",", p1.Concat(p2).Except(p1.Intersect(p2)));
OriginalGriff也打败了我,但无论如何都要发帖,因为它解决了其他几个问题。它保留升序并处理你在其中一个字符串中的尾随逗号。
OriginalGriff beat me too it, but posting mine anyhow, since it solves a couple of other issues. It preserves ascending order and deals with the trailing comma you have in one of your strings.
using System;
using System.Collections.Generic;
using System.Linq;
namespace DemoApp
{
public class Program
{
public static void Main(string[] args)
{
string string1 = "1,2,3,4";
string string2 = "2,3,5,9,10,";
// Convert strings to integer arrays to facilitate later sorting
int[] set1 = GetIntegers(string1);
int[] set2 = GetIntegers(string2);
// Get all of the numbers that appear in either set1 or set2
IEnumerable<int> union = set1.Union(set2);
// Get all of the numbers that appear in both set1 and set2
IEnumerable<int> intersection = set1.Intersect(set2);
// Get the list of numbers we want (union minus intersection) in ascending order
IEnumerable<int> unionExceptIntersection = union
.Except(intersection)
.OrderBy(value => value);
// Join the numbers back into a string
string result = string.Join(',', unionExceptIntersection);
Console.WriteLine(result);
}
private static int[] GetIntegers(string text) => text
// Split values around commas
.Split(',')
// Convert string values to integer values (or null)
.Select(strValue => int.TryParse(strValue, out int intValue) ? intValue : (int?)null)
// Eliminate invalid integer values
.Where(value => value.HasValue)
// Convert to non-nullable integer value
.Select(value => value.Value)
// Convert the sequence to an array (so we can iterate more than once)
.ToArray();
}
}
0)拆分
逗号分隔符上的字符串。
1)将每个数组的元素(从Split
语句)添加到HashSet
object(一个HashSet不允许重复的元素值)。
2)输出HashSet
对象的内容。
0)Split
the strings on comma delimiters.
1) Add each array's element (from theSplit
statement) to aHashSet
object (aHashSet will not allow duplicate element values).
2) Output the contents of theHashSet
object.
string[] parts1 = string1.Split(',', false);
string[] parts2 = string2.Split(',', false);
HashSet<string> hash = new HashSet<string>();
foreach(string part in parts1)
{
hash.Add(part);
}
foreach(string part in parts2)
{
hash.Add(part);
}
foreach(string part in hash)
{
Console.WriteLine(part);
}
Console.ReadKey();
使用 HashSet的好处
对象是,如果您尝试添加重复值,它不会抛出异常。相反,它根本不会添加它。 HashSet
不保证按任何顺序排列,因此我将其作为练习让程序员按所需顺序返回值。
The benefit of using a HashSet
object is that it will not throw an exception if you try to add a duplicate value. Instead, it simply won't add it. A HashSet
is not guaranteed to be in any order, so I leave it as an exercise for the programmer to return the values in the desired sequence.
这篇关于如何通过与另一个字符串进行比较来删除字符串中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文