[英]Changing a specific part of a string
在C#中,我得到了一個字符串,其格式如下:
a number|a number|a number,a number
例如:1 | 2 | 3,4
我認為每個數字都是字符串的不同部分。 在前面的示例中,1是第一部分,2是第二部分,依此類推。
給定要更改的部分的索引,我希望能夠替換字符串的特定部分。
用String.Split
做到這一點並不難,但是用逗號String.Split
部分使它變得乏味,因為那時我需要檢查索引是3還是4,然后還要用逗號分開。 有沒有更優雅的方法來切換字符串中的特定部分? 也許以某種方式使用正則表達式?
編輯:我將添加一些我以前沒有寫過的要求:
例如,如果我想取字符串的第三部分並將其替換為數字並添加2,該怎么辦?例如1 | 2 | 3,4到1 | 2 | 5,4,其中5不是常數但取決於給定的輸入字符串。
您可以創建以下方法
static string Replace(string input, int index, string replacement)
{
int matchIndex = 0;
return Regex.Replace(input, @"\d+", m => matchIndex++ == index ? replacement : m.Value);
}
用法:
string input = "1|2|3,4";
string output = Replace(input, 1, "hello"); // "1|hello|3,4
正如Eric Herlitz所建議的,您可以使用其他正則表達式,即定界符的負數。 例如,如果你希望,
並|
定界符,您可以將\\d+
替換為[^,|]+
正則表達式。 如果你希望,
, |
和#
分隔符,可以使用[^,|#]
正則表達式。
如果您需要進行一些數學運算,則可以自由進行:
static string Replace(string input, int index, int add)
{
int matchIndex = 0;
return Regex.Replace(input, @"\d+", m => matchIndex++ == index ? (int.Parse(m.Value) + add).ToString() : m.Value );
}
例:
string input = "1|2|3,4";
string output = Replace(input, 2, 2); // 1|2|5,4
您甚至可以使其通用:
static string Replace(string input, int index, Func<string,string> operation)
{
int matchIndex = 0;
return Regex.Replace(input, @"\d+", m => matchIndex++ == index ? operation(m.Value) : m.Value);
}
例:
string input = "1|2|3,4";
string output = Replace(input, 2, value => (int.Parse(value) + 2).ToString()); // 1|2|5,4
使用Regex.Split作為輸入,使用Regex.Match收集定界符
string input = "1|2|3,4,5,6|7,8|9";
string pattern = @"[,|]+";
// Collect the values
string[] substrings = Regex.Split(input, pattern);
// Collect the delimiters
MatchCollection matches = Regex.Matches(input, pattern);
// Replace anything you like, i.e.
substrings[3] = "222";
// Rebuild the string
int i = 0;
string newString = string.Empty;
foreach (string substring in substrings)
{
newString += string.Concat(substring, matches.Count >= i + 1 ? matches[i++].Value : string.Empty);
}
這將輸出"1|2|3,222,5,6|7,8|9"
試試這個(經過測試):
public static string Replace(string input, int value, int index)
{
string pattern = @"(\d+)|(\d+)|(\d+),(\d+)";
return Regex.Replace(input, pattern, match =>
{
if (match.Index == index * 2) //multiply by 2 for | and , character.
{
return value.ToString();
}
return match.Value;
});
}
用法示例:
string input = "1|2|3,4";
string output = Replace(input, 9, 1);
更新了新要求:
public static string ReplaceIncrement(string input, int incrementValue, int index)
{
string pattern = @"(\d+)|(\d+)|(\d+),(\d+)";
return Regex.Replace(input, pattern, match =>
{
if (match.Index == index * 2)
{
return (int.Parse(match.Value) + incrementValue).ToString();
}
return match.Value;
});
}
嘗試這個:
static void Main()
{
string input = "1|2|3|4,5,6|7,8|9|23|29,33";
Console.WriteLine(ReplaceByIndex(input, "hello", 23));
Console.ReadLine();
}
static string ReplaceByIndex(string input, string replaceWith, int index)
{
int indexStart = input.IndexOf(index.ToString());
int indexEnd = input.IndexOf(",", indexStart);
if (input.IndexOf("|", indexStart) < indexEnd)
indexEnd = input.IndexOf("|", indexStart);
string part1 = input.Substring(0, indexStart);
string part2 = "";
if (indexEnd > 0)
{
part2 = input.Substring(indexEnd, input.Length - indexEnd);
}
return part1 + replaceWith + part2;
}
這是假設數字按升序排列。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.