C#创建一个字符串,该字符串包含字符串中两个字符之间的所有内容
所以我有一个程序,需要得到一个字符串,它在一个字符串中,在两个点之间C#创建一个字符串,该字符串包含字符串中两个字符之间的所有内容,c#,string,C#,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(
{
和}
我正在使用代码
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
},
我必须能够对它们进行排序。如果你仔细想想,这就是为什么我在方法中有主题名称。看起来你试图在一个方法中实现两个想法。你在搜索开始和结束大括号并搜索主题。如果你已经知道主题,为什么要搜索它?然而,这里有一个方法I’我将得到括号内的主题
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;
}
看起来您试图在一个方法中实现两个想法。您正在搜索开始和结束大括号并搜索主题。如果您已经知道主题,为什么要搜索它?但是,下面有一个方法可以获取大括号内的主题
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;
}
我将用一个例子来说明实现中的一些缺陷 假设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
然后:
然后:
然后:
然后:
然后:
我将用一个例子来说明实现中的一些缺陷 假设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
然后:
然后:
然后:
然后:
然后:
错误在于
tend
是之后中的索引,而不是sourceString
中的索引!将t1
添加到其中以获得绝对索引。同时将1添加到子字符串的长度中以包含最后一个“}”
如果在搜索中包含主题周围的引号也会更安全:
int t1 = sourceString.IndexOf("\"" + topicName + "\"");
但是,正如@EugenePodskal在他的评论中指出的那样,如果没有可靠的语法分析,代码仍然是冒险的。错误在于tend
是之后中的索引,而不是sourceString
中的索引!将t1
添加到其中以获得绝对索引。还将1添加到子字符串的长度字符串,以便包含最后一个“}”
如果在搜索中包含主题周围的引号也会更安全:
int t1 = sourceString.IndexOf("\"" + topicName + "\"");
但是,正如@EugenePodskal在他的评论中指出的那样,如果没有可靠的语法分析,代码仍然是冒险的。我建议您使用一些javascript AST解析器-。这看起来可能有点过分,但对于数据解析来说,最好是安全的,而不是抱歉。我建议您使用一些javascript AST解析器-。这看起来可能有些过分,但使用数据解析时,最好是安全的,而不是抱歉。我已经知道这种方法。但是这个程序为游戏创建mod。所以有很多{and}的打开和关闭,所以这个方法会中断,不能正常工作。我已经知道这个方法了。但是这个程序为游戏创建mod。所以有很多{and}的打开和关闭,所以这个方法会中断,不能正常工作。
int tend = t1 + after.IndexOf("}");
string topic = sourceString.Substring(tstart, tend - tstart + 1);
int t1 = sourceString.IndexOf("\"" + topicName + "\"");