C#创建一个字符串,用于保存字符串中两个字符之间的所有内容

时间:2016-02-21 21:36:37

标签: 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(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([
        ]);
    };

})();

并且想要找到一个主题。在这种情况下,名称是"随机主题" 假设通过查找主题的名称从该字符串中获取此字符串:

            {
            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
            },

但它返回的只是{

我做错了什么?

编辑: 该程序为游戏创建mods,因此有多个

副本
            {
            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
            },

我必须能够对它们进行分类。如果您考虑这就是为什么我在方法中有主题名称。

3 个答案:

答案 0 :(得分:3)

看起来你试图在一种方法中实现两个想法。您正在搜索开始和结束括号并搜索主题。如果您已经知道该主题,为什么要搜索它?但是,这里有一个方法可以获得大括号内的主题。

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

答案 1 :(得分:1)

错误是tendafter内的索引,而不是sourceString内的索引!添加t1以获取绝对索引。还要在子字符串的长度上加1,以便包含最后一个“}”。

int tend = t1 + after.IndexOf("}");
string topic = sourceString.Substring(tstart, tend - tstart + 1);

如果在搜索中包含主题的引号也更安全:

int t1 = sourceString.IndexOf("\"" + topicName + "\"");

但是@EugenePodskal在他的评论中已经指出,如果没有扎实的语法分析,代码仍然具有冒险性。

答案 2 :(得分:0)

我将通过一个例子来说明实现中的一些缺陷。

让我们说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