[英]C# creating a string that holds a everything in between two characters that are in a string
因此,我有一個程序需要在字符串中以及兩點{
和}
之間獲取一個字符串
我正在使用代碼
public string findTopic(string sourceString, string topicName)
{
//Finds the point where the topic name is and cuts everything off infront of it.
int t1 = sourceString.IndexOf(topicName);
string before = sourceString.Substring(0, t1);
//Finds the { that opens the topic
int tstart = before.LastIndexOf("{");
//Finds the end of the topic
string after = sourceString.Substring(t1);
//Finds the } that closes the topic
int tend = after.IndexOf("}");
string topic = sourceString.Substring(tstart, tend - tstart);
Console.WriteLine(before);
Console.WriteLine(after);
Console.WriteLine(t1.ToString());
Console.WriteLine(tstart.ToString());
Console.WriteLine(tend.ToString());
Console.WriteLine("Topic Found = " + topic);
return topic;
}
這只會給我{
它正在通過一個看起來像這樣的字符串
var Ultimate_Mod_Maker_Mod = {};
(function () {
//Made_with_Ultimate_Mod_Maker
Ultimate_Mod_Maker_Mod.addTopic = function () {
GDT.addTopics([
{
id: "4235-1405-1469-567-4280",//ID
name: "Random Topic".localize("game topic"),//Name
genreWeightings: [0.9, 0.9, 0.9, 0.9, 0.9, 0.9],//Genre
audienceWeightings: [0.9, 0.9, 0.9]//Audience
},
]);
};
Ultimate_Mod_Maker_Mod.addPlatform = function () {
GDT.addPlatforms([
]);
};
})();
並且應該找到一個主題。 在這種情況下,名稱為“ Random Topic”。假定通過找到主題名稱從該字符串中獲取此字符串:
{
id: "4235-1405-1469-567-4280",//ID
name: "Random Topic".localize("game topic"),//Name
genreWeightings: [0.9, 0.9, 0.9, 0.9, 0.9, 0.9],//Genre
audienceWeightings: [0.9, 0.9, 0.9]//Audience
},
但它返回的只是{
我究竟做錯了什么?
編輯:此程序為游戲創建mod,因此有多個副本
{
id: "4235-1405-1469-567-4280",//ID
name: "Random Topic".localize("game topic"),//Name
genreWeightings: [0.9, 0.9, 0.9, 0.9, 0.9, 0.9],//Genre
audienceWeightings: [0.9, 0.9, 0.9]//Audience
},
我必須能夠對它們進行排序。 如果您考慮這就是為什么我在方法中使用主題名稱。
似乎您正在嘗試用一種方法實現兩個想法。 您正在搜索起始括號和結尾括號,並正在搜索主題。 如果您已經知道該主題,為什么要搜索它? 但是,這里有一種方法可以獲取括號內的主題。
public string findTopic2(string sourceString)
{
int start = sourceString.IndexOf("{") + 1;
//Finds the } that closes the topic
int end = sourceString.IndexOf("}");
string topic = sourceString.Substring(start, end - start);
return topic;
}
錯誤的是, tend
是內部的指標after
,未中的索引sourceString
! 添加t1
以獲得絕對索引。 還要在子字符串的長度上加1,以包括最后一個“}”。
int tend = t1 + after.IndexOf("}");
string topic = sourceString.Substring(tstart, tend - tstart + 1);
如果在搜索中包含該主題周圍的引號也更安全:
int t1 = sourceString.IndexOf("\"" + topicName + "\"");
但是由於@EugenePodskal在其評論中已經指出,因此如果不進行扎實的語法分析,代碼仍然很冒險。
我將舉一個例子來說明實現中的一些缺陷。
可以說sourceString是:
Index | 0 1 2 3 4 5 6 7 8 9
Char | M y { R e d } D o g
然后,讓我們說topicName = "My"
。 然后:
t1
= sourceString.IndexOf(topicName) * definition of t1
= sourceString.IndexOf("My") * definition of topicName
= "My{Red}Dog".IndexOf("My") * definition of sourceString
= 0 * evaluate IndexOf
然后:
before
= sourceString.Substring(0, t1) * definition of before
= sourceString.Substring(0, 0) * 0 = t1
= "My{Red}Dog".Substring(0, 0) * definition of sourceString
= "" * evaluate Substring
然后:
tstart
= before.LastIndexOf("{") * definition of tstart
= "".LastIndexOf("{") * "" = before
= -1 * evaluate LastIndexOf
然后:
after
= sourceString.Substring(t1) * definition of after
= sourceString.Substring(0) * 0 = t1
= "My{Red}Dog".Substring(0) * definition of sourceString
= "My{Red}Dog" * evaluate Substring
然后:
tend
= after.IndexOf("}") * definition of tend
= "My{Red}Dog".IndexOf("}") * "My{Red}Dog" = tend
= 6 * evaluate IndexOf
然后:
topic
= sourceString.Substring(tstart, tend - tstart) * definition of topic
= sourceString.Substring(-1, tend - (-1)) * -1 = tstart
= sourceString.Substring(-1, 6 - (-1)) * 6 = tend
= "My{Red}Dog".Substring(-1, 6 - (-1)) * definition of sourceString
= "My{Red}Dog".Substring(-1, 7) * 7 = 6 - (-1)
= undefined * evaluate Substring
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.