Advertisement

Ho to Split a string by Multiple tag then process it one by one

Started by November 21, 2019 08:18 AM
10 comments, last by Sylon87 5 years, 2 months ago

i'm looking for a way to split a string of multiple tag into a separate string to process it one by one for example when a string og tag "[{M}sometag] [sometag] [{F}sometag]" is coming is should separate firstly from the "[" then leave just {M}sometagand process one by one.... any idea?

 

i know that i should use a for loop to loop through the string char but i can't figure out the algortihm

since you did not mention a language. In Java or C# :  string.split('[') should work.

In javaScript there is something like substring(-1) to cut of the last ']'. Otherwise use substring(length-1) or so.

Also there is regex.Match("[\[%\]]"). Hah, I need regex only once a year. Sorry something along these lines.

var lastOccurance=-1


for(var i=0;i<length;i++){

if (string=="[") lastOccurance=i

} 

 

Advertisement
3 minutes ago, arnero said:

since you did not mention a language. In Java or C# :  string.split('[') should work.

In javaScript there is something like substring(-1) to cut of the last ']'. Otherwise use substring(length-1) or so.

Also there is regex.Match("[

]"). Hah, I need regex only once a year. Sorry something along these lines.

 

var lastOccurance=-1

for(var i=0;i<length;i++){

if (string=="[") lastOccurance=i

}

yes sorre, would be C#, well i'm getting totally confused at this point, i should take out the word contained between the two "[ ]" replace that tag then append it to a list of string, i think i should process it char by char.. 

I tried to give you some ideas. I am not doing your homework. I do not know why you think you should do something "char by char". In the real world I would expect programmers in C# to almost never do such bare metal things.

41 minutes ago, arnero said:

I tried to give you some ideas. I am not doing your homework. I do not know why you think you should do something "char by char". In the real world I would expect programmers in C# to almost never do such bare metal things.

Sorry but i’m not asking to do a homework because it is not a homework.

 

i’m trying to create a dialogue system where when i have the string 

 

string s = “hello [Tag] how are you? I’m fine anda you ms,[tag]?”

I have to replace the tag with corresponding words i have already a methods to do that i’m looking for a way to get off the string only the tag replace that word then rebuild the entire string.

 

 

Oh, sorry for my acusation.

Maybe you could switch to a standard syntax?
https://stackoverflow.com/questions/16098177/replacing-multiple-placeholders-in-a-string

Or adapt using string.Replace ?
 

JavaScript has built in almost the same syntax: https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/template_strings

Ah, okay I see the problem. A great advancement in computer science was to use labels instead of memory adresses. Some people forgot about that and created functions with looooong parameter lists. Luckily now we have got named parameters in C#. But then comes along this stupid template string syntax in C#. Javascript and razor (for C#) are better suited. You are not -- by chance -- writing a WebApp?

I mean I once inherited code which used String.split() for everything. You should start with split, look at the result and then trim (not really the "trim" function, but substring) the strings. It is simpler to debug than working with cursors/index into a looong string.

Advertisement
13 hours ago, arnero said:

Oh, sorry for my acusation.

Maybe you could switch to a standard syntax?
https://stackoverflow.com/questions/16098177/replacing-multiple-placeholders-in-a-string

Or adapt using string.Replace ?
 

JavaScript has built in almost the same syntax: https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/template_strings

Ah, okay I see the problem. A great advancement in computer science was to use labels instead of memory adresses. Some people forgot about that and created functions with looooong parameter lists. Luckily now we have got named parameters in C#. But then comes along this stupid template string syntax in C#. Javascript and razor (for C#) are better suited. You are not -- by chance -- writing a WebApp?

I mean I once inherited code which used String.split() for everything. You should start with split, look at the result and then trim (not really the "trim" function, but substring) the strings. It is simpler to debug than working with cursors/index into a looong string.

Thank’s you for your advice!  I’ll try and let you know the results

 

again thank’s you!

You can likely also search for "[tag]", get its position, break up the string in pieces (before and after), and construct a new string from the pieces,

is done


  public string Extract(string in_tag)
    {
        string current  = in_tag;
        bool isCandidate = false;
        string tempstring;
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < current.Length; i++)
        {
            char c= current[i];

            if(c== '[')
            {
                isCandidate = true;
            }

            if(isCandidate)
            {
                result.Append(c);
                if(c == ']')
                {
                    tempstring = result.ToString();
                    result.Clear();
                    for (int j = 0; j < _entriesAry.Length; j++)
                    {
                        if(_entriesAry[j].IsMatchTag(tempstring))
                        {
                            current.Replace(tempstring,_entriesAry[j].ReplacedWord());
                        }
                    }
                    isCandidate = false;
                }
            }
        }

        return current;
    }

 

You should use the StringBuilder until the end and then return the contents of it instead of constructing a string, then save that string and propably repeat the same process over and over again. If you want some more performance and/or readbility, you could in this case also use a switch statement instead


for(int i = 0; i < inpit.Length; i++)
    switch(input[i])
    {
        case '[':
           {
                if(inTag > 0) tagBuffer.Append(input[i]);
                else tagBuffer.Clear();
                inTag++;
           }
           break;
        case ']':
           {
                if(--inTag == 0) textBuffer.Apped(ResolveTag(tagBuffer.ToString());
                else tagBuffer.Append(input[i]);
           }
           break;
        default:
           {
               if(inTag > 0) tagBuffer.Append(input[i]);
               else textBuffer.Append(input[i]);                                   
           }
           break;
    }

It is just an improvement ☺️

This topic is closed to new replies.

Advertisement