[英]how to manipulate string which contains different pattern in C#?
我有以下類型的字符串格式-
Proposal is given to {Jwala Vora#3/13} for {Amazon Vally#2/11} {1#3/75} by {MdOffice employee#1/1}
該字符串包含一對{} ,它們的位置不同,並且可以為n次。 現在,我想用將根據{}對之間的字符串進行計算的其他字符串替換該對。
這個怎么做 ?
您可以嘗試使用正則表達式。 具體來說,使用MatchEvaluator
Regex.Replace
變體應該可以解決問題。 有關更多信息,請參見http://msdn.microsoft.com/zh-CN/library/cft8645c(v=vs.80).aspx 。
遵循以下原則:
using System;
using System.Text.RegularExpressions;
public class Replacer
{
public string Replace(string input)
{
// The regular expression passed as the second argument to the Replace method
// matches strings in the format "{value0#value1/value2}", i.e. three strings
// separated by "#" and "/" all surrounded by braces.
var result = Regex.Replace(
input,
@"{(?<value0>[^#]+)#(?<value1>[^/]+)/(?<value2>[^}]+)}",
ReplaceMatchEvaluator);
return result;
}
private string ReplaceMatchEvaluator(Match m)
{
// m.Value contains the matched string including the braces.
// This method is invoked once per matching portion of the input string.
// We can then extract each of the named groups in order to access the
// substrings of each matching portion as follows:
var value0 = m.Groups["value0"].Value; // Contains first value, e.g. "Jwala Vora"
var value1 = m.Groups["value1"].Value; // Contains second value, e.g. "3"
var value2 = m.Groups["value2"].Value; // Contains third value, e.g. "13"
// Here we can do things like convert value1 and value2 to integers...
var intValue1 = Int32.Parse(value1);
var intValue2 = Int32.Parse(value2);
// etc.
// Here we return the value with which the matching portion is replaced.
// This would be some function of value0, value1 and value2 as well as
// any other data in the Replacer class.
return "xyz";
}
}
public static class Program
{
public static void Main(string[] args)
{
var replacer = new Replacer();
var result = replacer.Replace("Proposal is given to {Jwala Vora#3/13} for {Amazon Vally#2/11} {1#3/75} by {MdOffice employee#1/1}");
Console.WriteLine(result);
}
}
此程序將輸出Proposal is given to xyz for xyz xyz by xyz
。
您需要在ReplaceMatchEvaluator
方法中提供特定於應用程序的邏輯,以ReplaceMatchEvaluator
地處理value0
, value1
和value2
。 類Replacer
可以包含其他成員,這些成員可用於實現ReplaceMatchEvaluator
的替換邏輯。 通過在Replacer
類的實例上調用Replace
處理字符串。
好了,您可以將字符串除以'{'和'}',然后以這種方式確定內容。
但是我認為更好的方法是按索引查找字符,然后知道一對或花括號的開始索引和結束索引,以便您可以用替換的占位符來重建字符串。
但是最好的方法可能是使用Regex.Replace,但這只會幫助將占位符替換為您想要的值,但我認為您的要求是還要解析大括號內的文本,並基於此選擇要插入的值,這可能效果不好。 查找並用通配符類型搜索替換字符串的一部分
您可以使用Regex.Replace方法(字符串,字符串,MatchEvaluator)方法和{.*?}
模式。 以下示例使用字典替換值,但是您可以使用自己的邏輯替換它。
class Program
{
static Dictionary<string, string> _dict = new Dictionary<string, string>();
static void Main(string[] args)
{
_dict.Add("{Jwala Vora#3/13}","someValue1");
_dict.Add("{Amazon Vally#2/11}", "someValue2");
_dict.Add("{1#3/75}", "someValue3");
_dict.Add("{MdOffice employee#1/1}", "someValue4");
var input = @"Proposal is given to {Jwala Vora#3/13} for {Amazon Vally#2/11} {1#3/75} by {MdOffice employee#1/1}";
var result = Regex.Replace(input, @"{.*?}", Evaluate);
Console.WriteLine(result);
}
private static string Evaluate(Match match)
{
return _dict[match.Value];
}
}
你不能用string.Format()
做點什么嗎?
例如
string.Format("Proposal is given to {0} for {1} {2} by {3}", "Jwala Vora", "Amazon Vally", 1, "MdOffice employee");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.