2013-03-13 44 views
1

我在問一個很常見的功能,我找不到任何有關信息。我想讓我的程序用戶使用程序中的變量創建自定義字符串。允許用戶定義字符串在c#

例子:

我有一個列表:

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}該項目的名稱是{Name}。它的描述是{Description}。它具有值{值}和狀態{狀態}。

我可以爲這些字符串編寫自定義解析例程,但我希望找到該任務的標準解決方案。有什麼建議麼?

+0

您是否考慮過使用String.Replace,String.Format或Regex?密鑰(名稱,說明,價值和狀態)是否已修復? – 2013-03-13 08:44:52

+0

除了_pretty common_這個事實,恐怕你必須編寫你自己的解析例程。您必須制定規則,檢查類型轉換等。是不是要求使用預定義GUI的數據更容易? – 2013-03-13 08:46:04

+0

String.Replace做的伎倆。 @JohnWillemse:好的,我認爲這是一個普遍的特徵可能是一種誤解。我在自動化領域工作,在這些領域中,您經常使用參數來創建信息字符串,這些參數對我來說很常見。 :) – twittfort 2013-03-13 14:03:09

回答

0

您可能允許在字符串中指定的「變量」用戶使用。例如:

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); 
+0

這正是我需要的。如果找到該參數,請將其替換。對我來說很完美。 – twittfort 2013-03-13 13:59:24

3

C#中的字符串格式化標準解決方案(通常在.NET中)是使用the String.Format method。所以,你可以例如做:

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); 
+0

這是可以的,如果你在開發過程中建立字符串。但我想給用戶設計自己的字符串的可能性。參數的位置和數量可能會有所不同。 – twittfort 2013-03-13 14:00:50

1

你只是說:

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); 

還是我錯過了點?

2

我懷疑你想用你的對象列表中的值替換佔位符,例如{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或自定義對象,或任何其他容器的屬性。

當然,這個解決方案不包含任何錯誤處理(例如屬性不存在的佔位符),如果允許用戶指定字符串,我想你會包含這個錯誤處理。

0

你可以定義你自己的類和重寫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); 
    } 
} 
相關問題