0

So I have a program that needs to get a string thats in a string and between two points { and }

I am using the code

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

This gives me only {

It is going through a string that looks like this

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

})();

And it is suppose to find the a topic. In this case the name is "Random Topic" It is suppose to get this string from that string by finding the name of the 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
            },

But all it returns is {

What am I doing wrong?

Edit: This program creates mods for games, so there are multiple copies of the

            {
            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 have to be able to sort through them. If you think about it thats why I have the topic name in the method.

  • 1
    I would recommend you to use some javascript AST parser - http://stackoverflow.com/questions/14355910/javascript-parser-and-analyzer-in-c-sharp-net-4-5. It may look like an overkill, but with data parsing it is better be safe than sorry. – Eugene Podskal Feb 21 '16 at 21:53

3 Answers3

3

It looks like you were trying to implement two ideas in one method. You were searching for the start and end braces and searching for the topic. If you already know the topic, why search for it? However, here is a method that will get the topic that is inside the braces.

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;
}
  • I already know of this method. But this program creates mods for games. So there are many opening and closing of { and } so this method would break and not work properly. – Swiftly Team Feb 22 '16 at 02:14
1

The error is that tend is the index within after, not the index within sourceString! Add t1 to it in order to get the absolute index. Also add 1 to the length of the substring in order to include the last "}".

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

If would also be safer to include the quotes around the topic in the search:

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

But the code remains adventurous without a solid syntax analysis as @EugenePodskal already pointed out in his comment.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
0

I will work one example to illustrate some flaws in the implementation.

Lets say sourceString is:

Index | 0 1 2 3 4 5 6 7 8 9
Char  | M y { R e d } D o g

Then lets say topicName = "My". Then:

t1
= sourceString.IndexOf(topicName)  * definition of t1
= sourceString.IndexOf("My")       * definition of topicName
= "My{Red}Dog".IndexOf("My")       * definition of sourceString
= 0                                * evaluate IndexOf

Then:

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

Then:

tstart
= before.LastIndexOf("{")          * definition of tstart
= "".LastIndexOf("{")              * "" = before
= -1                               * evaluate LastIndexOf

Then:

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

Then:

tend
= after.IndexOf("}")               * definition of tend
= "My{Red}Dog".IndexOf("}")        * "My{Red}Dog" = tend
= 6                                * evaluate IndexOf

Then:

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
erisco
  • 14,154
  • 2
  • 40
  • 45