我有一个看起来像这样的字符串数组
var input = new [] { "AB-PQ", "PQ-EF", "EF=CD", "CD-IJ", "IJ=XY", "XY-JK" };
我想要一个类似这样的字符串输出
var output = "AB-PQ-EF=CD-IJ=XY-JK"
我想知道是否有更好的方法,而不是使用蛮力循环,然后使用字符串生成器进行拆分和组合。
我的业务用例: 我的业务用例是,给定的字符串表示包含多个城市的路线中两个城市之间的链接序列。 “-”表示道路连接,“ =”表示铁路连接。城市代码可以是任意长度。
答案 0 :(得分:1)
这有效:
var input = new [] { "AB-PQ", "PQ-EF", "EF=CD", "CD-IJ", "IJ=XY", "XY-JK" };
var output = String.Join("",
input.Take(1).Concat(new [] { String.Join("", input.Skip(1).Select(x => x.Substring(2))) }));
我不喜欢它,但是它可以工作。它产生"AB-PQ-EF=CD-IJ=XY-JK"
。
尝试以下更可靠的方法:
void Main()
{
var input = new[] { "AB-PQ", "PQ-XYZ", "XYZ=CD", "CD-A", "A=XY", "XY-JK" };
var output =
input
.Select(i => new Segment(i))
.Aggregate(
"",
(a, x) => a + x.ToString().Substring(a == "" ? 0 : x.Origin.Length));
}
public enum Mode
{
Road, Rail
}
public sealed class Segment : IEquatable<Segment>
{
private readonly string _origin;
private readonly Mode _mode;
private readonly string _destination;
public string Origin { get { return _origin; } }
public Mode Mode { get { return _mode; } }
public string Destination { get { return _destination; } }
public Segment(string descriptor)
{
var parts = descriptor.Split('-', '=');
if (parts.Length != 2)
{
throw new System.ArgumentException("Segment descriptor must contain '=' or '-'.");
}
_origin = parts[0];
_mode = descriptor.Contains("=") ? Mode.Rail : Mode.Road;
_destination = parts[1];
}
public Segment(string origin, Mode mode, string destination)
{
_origin = origin;
_mode = mode;
_destination = destination;
}
public override bool Equals(object obj)
{
if (obj is Segment)
return Equals((Segment)obj);
return false;
}
public bool Equals(Segment obj)
{
if (obj == null) return false;
if (!EqualityComparer<string>.Default.Equals(_origin, obj._origin)) return false;
if (!EqualityComparer<Mode>.Default.Equals(_mode, obj._mode)) return false;
if (!EqualityComparer<string>.Default.Equals(_destination, obj._destination)) return false;
return true;
}
public override int GetHashCode()
{
int hash = 0;
hash ^= EqualityComparer<string>.Default.GetHashCode(_origin);
hash ^= EqualityComparer<Mode>.Default.GetHashCode(_mode);
hash ^= EqualityComparer<string>.Default.GetHashCode(_destination);
return hash;
}
public override string ToString()
{
return $"{_origin}{(_mode == Mode.Rail ? "=" : "-")}{_destination}";
}
public static bool operator ==(Segment left, Segment right)
{
if (object.ReferenceEquals(left, null))
{
return object.ReferenceEquals(right, null);
}
return left.Equals(right);
}
public static bool operator !=(Segment left, Segment right)
{
return !(left == right);
}
}
答案 1 :(得分:0)
您可以像这样使用LINQ和 // GetJobsFromTable() runs fine and I am getting jobs to execute
var dt = GetJobsFromTable();
Parallel.For(0, dt.Rows.Count, i => {
var item = dt.Rows[i];
ProcessJob(item);
});
:
string.Join
别忘了首先将其添加到您的var input = new[]{"AB-PQ", "PQ-EF", "EF=CD", "CD-IJ", "IJ=XY", "XY-JK"};
var result = input[0] + string.Join("", input.Skip(1)
.Select(c => string.Join("", c.Skip(2))));
指令中:
using
答案 2 :(得分:0)
另一个版本:
var input = new [] {"AB-PQ", "PQ-EF", "EF=CD", "CD-IJ", "IJ=XY", "XY-JK"};
var output = string.Join("=", string.Join("-", input).Split('=').Select(x => string.Join("-", x.Split('-').Distinct())))
答案 3 :(得分:0)
var input = new [] { "AB-PQ", "PQ-EF", "EF=CD", "CD-IJ", "IJ=XY", "XY-JK" };
var r = input.Aggregate((total, current) =>
total + current.Substring(OverlappingCharCount(total, current))
);
您可以定义OverlappingCharCount
方法以始终返回2(例如,在示例中,字符串始终重叠2个字符),或使用一些更智能的逻辑来处理更多通用情况。
答案 4 :(得分:0)
使用Regex达到相同的目的
string str = string.Join("-", input);
Regex oRegex = new Regex(@"\b(?<Captured>\w+)-\1\b",RegexOptions.IgnoreCase);
oRegex.Matches(str).Cast<Match>().ToList().ForEach(items=>str = str.Replace(items.ToString(), items.Groups["Captured"].ToString()));
Console.WriteLine(str);
答案 5 :(得分:0)
这是完整的答案
GUI.DrawTexture()