![](/img/trans.png)
[英]What's the easiest way to change the contents of text in a string with C#?
[英]String operation — Easiest way C#
实施以下方案的最佳方法是什么
string sample = "Mountain View XX,Lake"
预计2种情况为o / p
山景湖
我可以通过sample.split[','][1]
到达湖泊
什么是让Mountain View ignoring XX
的最佳方法是什么
我尝试了多个拆分并将它们也与lastIndex串联在一起 !
那么还有其他更简单的方法吗?
string.Substring
方法可以正常工作:
sample = sample.Substring(0, sample.IndexOf(","));
还有一个string.LastIndexOf
,直到子字符串的最后一次出现,例如:
sample = sample.Substring(0, sample.LastIndexOf(","));
在您的样品,都将在相同的工作,因为只有一个,
焦炭。
之后,您可以删除直到最后 空格字符。
sample = sample.Substring(0, sample.LastIndexOf(" "));
sample= sample.Replace("XX", "");
尝试使用正则表达式。 (\\ w + \\ s +?\\ w +)\\ s +?XX \\,(\\ w +) http://msdn.microsoft.com/zh-cn/library/system.text.regularexpressions.regex.aspx组1包含Mountain View
组2包含Lake
您说您尝试过多次拆分。 您可以在单个string.Split()
指定多个分隔符:
var parts
= sample.Split(new[] { "XX", "," }, StringSplitOptions.RemoveEmptyEntries);
现在您有两个部分:
var part1 = parts[0]; // Mountain View
var part2 = parts[1]; // Lake
目前尚不清楚为什么要删除字符串的确切部分,但是这里有一些不同的方法可以删除字符串:
string sample = "Mountain View XX,Lake"
// option 1
string removed = sample.Remove(14, 3);
// option 2
string replaced = sample.Replace("XX,", String.Empty);
// option 3
string[] parts = sample.Split(',');
parts[0] = parts[0].Substring(0, 14);
string concatenated = String.Concat(parts);
如果字符串的XX
部分确实是其他内容,例如街道编号,那么您将需要一些内容来确定要删除多少字符串。 这里有些例子:
string sample = "Mountain View 123,Lake"
// option 1; remove digits followed by a comma
string replaced = Regex.Replace(sample, "\d+,", String.Empty);
// option 2; remove what's after the last space in the part before the comma
string[] parts = sample.Split(',');
parts[0] = parts[0].Substring(0, parts[0].LastIndexOf(" ") + 1);
string concatenated = String.Concat(parts);
你遗漏了很多东西。 XX一定是XX吗? 您是否保证格式正确,或者您信任用户输入? 如果您知道想要所有开头字符,直到遇到双大写字母组合,则可以执行一个非常简单的正则表达式。 我必须查找它,因为我已经使用它们已有多年了,但这将非常简单。
这可以解决问题。 它确实包括两个XX之前的空格。 我假设XX可以是任何大写字母。
Regex reg;
reg = new Regex(".{1,}(?=[A-Z]{2})");
var output = reg.Match("Mountain View XX, Lake");
string text = output.Value;
您可以在此处进行测试: http : //www.regexplanet.com/advanced/dotnet/index.html,并在此处了解有关正则表达式的更多信息: http : //www.regular-expressions.info/tutorial.html
我建议您选择:
string sample = "Mountain View XX,Lake";
Console.WriteLine(sample.Split(',')[1]);// Lake
Console.WriteLine(sample.Remove(sample.IndexOf("XX,")).Trim()); // Mountain View
演示: http : //ideone.com/RizzzC
我可能会做这样的事情:
public static string[] MyCustomSplit(string input, List<string> reservedWords)
{
List<string> outputLines = new List<string>();
string[] lines = input.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string line in lines)
{
if (!string.IsNullOrWhiteSpace(line))
{
string[] parts = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
List<string> lineParts = new List<string>();
foreach (string part in parts)
{
if (!reservedWords.Contains(part))
{
lineParts.Add(part);
}
}
outputLines.Add(string.Join(" ", lineParts.ToArray()));
}
}
return outputLines.ToArray();
}
接着
string sample = "Mountain View XX,Lake";
List<string> reservedWords = new List<string>() { "XX" };
string[] test = MyCustomSplit(sample, reservedWords);
结果是:
string[0] = Mountain View
string[1] = Lake
或类似这样的东西:
public static string[] MyCustomSplitAndCleanup(string input)
{
List<string> outputLines = new List<string>();
string[] lines = input.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string line in lines)
{
if (!string.IsNullOrWhiteSpace(line))
{
string[] parts = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
List<string> lineParts = new List<string>();
for (int index = 0; index < parts.Length; index++ )
{
string part = parts[index];
int numericValue = 0;
bool validPart = true;
if (int.TryParse(part, out numericValue))
{
if (index == 0 || index == parts.Length - 1)
{
validPart = false;
}
}
if (validPart)
{
lineParts.Add(part);
}
}
outputLines.Add(string.Join(" ", lineParts.ToArray()));
}
}
return outputLines.ToArray();
}
涵盖了这一点:
string sample1 = "Mountain View 32,Lake";
string sample2 = "17 Park place,Something";
string[] test1 = MyCustomSplitAndCleanup(sample1);
string[] test2 = MyCustomSplitAndCleanup(sample2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.