在c#中允许用户定义字符串
本文关键字:用户 定义 字符串 许用户 | 更新日期: 2023-09-27 18:03:43
我正在询问一个非常常见的功能,我找不到任何有关的信息。我希望允许程序的用户使用程序中的变量创建自定义字符串。
的例子:
我有一个列表:
ID Name Description Value Status
0 name 0 beschreibung 0 Wert 0 Status 0
1 name 1 beschreibung 1 Wert 1 Status 1
2 name 2 beschreibung 2 Wert 2 Status 2
3 name 3 beschreibung 3 Wert 3 Status 3
4 name 4 beschreibung 4 Wert 4 Status 4
5 name 5 beschreibung 5 Wert 5 Status 5
6 name 6 beschreibung 6 Wert 6 Status 6
7 name 7 beschreibung 7 Wert 7 Status 7
8 name 8 beschreibung 8 Wert 8 Status 8
9 name 9 beschreibung 9 Wert 9 Status 9
现在用户将能够定义自定义字符串,如:
Id为{Id}的Item的Name为{Name}。它的描述是{Description}。取值为{Value},状态为{Status}。
我可以为这些字符串编写一个自定义解析例程,但我希望为该任务找到一个标准解决方案。有什么建议吗?
c#(以及一般的。net)中字符串格式化的标准解决方案是使用string。格式的方法。例如,你可以这样写:
string reult = string.Format("The Name of the Item with the Id {0} is {1}. It's Description is {2}. It has the Value {3} and the Status {4}.",
id, name, description, value, status);
我怀疑您想用对象列表中的值替换占位符,例如{ID}
和{Name}
。您可以使用带有
'{(?<key>[A-Za-z]+)'}
这将找到{something}
的所有实例,并允许您提取something
以便从列表中获得值。
使用Regex.Match
的重载,它接受一个输入字符串和一个MatchEvaluator
委托,将允许你得到正确的值:
var myObject = new Dictionary<string,string>(){
{"ID","1"},
{"Name","Bob Jones"},
{"Description","A very nice man"},
{"Value","1000"},
{"Status","New"},
};
var regex = new Regex(@"'{(?<key>[A-Za-z]+)'}");
var input = "The Name of the Item with the Id {ID} is {Name}. It's Description is {Description}. It has the Value {Value} and the Status {Status}.";
string result = regex.Replace(input, m => myObject[m.Groups["key"].Value]);
Console.WriteLine(result);
实例:http://rextester.com/FLGTQW95860
那个例子中的字典只是为了演示正则表达式的用法——没有理由不能是DataRow
或自定义对象,或任何其他属性的容器。
当然,此解决方案不包含任何错误处理(例如属性不存在的占位符),如果您允许用户指定字符串,我怀疑您将希望包含错误处理。
你的意思是:
var x = String.Format("The Name of the Item with the Id {0} is {1}. It's Description is {2}. It has the Value {3} and the Status {4}.",object.Id, object.Name, object.Description, object.Value, object.Status);
还是我没抓住重点?
您可以允许用户在字符串中使用指定的"变量"。例如:
var userstring = "The Name of the Item with the Id {ID} is {Name}. It's Description is {Description}. It has the Value {Value} and the Status {Status}.";
var result = userstring.Replace("{ID}", Id).Replace("{Name}", Name).Replace("{Description}", Description).Replace("{Value}", Value).Replace("{Status}", Status);
你可以定义你自己的类并覆盖ToString:
public class Program
{
static void Main(string[] args)
{
var aString = new MyString() {Description = "desc", Id = 1, Name = "Ja", Status = "st", Value = "Val"};
var myStrings = new List<MyString>() {aString};
foreach (MyString myString in myStrings)
{
//The Name of the Item with the Id 1 is Ja. It's Description is desc. It has the Value val and the Status st.
var outputStringVal = myString.ToString();
//
}
}
}
public class MyString
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Value { get; set; }
public string Status { get; set; }
public override string ToString()
{
return String.Format("The Name of the Item with the Id {0} is {1}. It's Description is {2}. It has the Value {3} and the Status {4}.", this.Id, Name, Description, Value, Status);
}
}