Home > Net >  C# Something messed up with this function spilt()
C# Something messed up with this function spilt()

Time:07-22

I'm stack with this function spilt()

As you know via this function spilt() you can get any string between two strings, words, etc..

I read a lot about this function https://docs.microsoft.com/ spilt() Method

And I read many topics on Stackoverflow and Codeproject, watched many videos on Youtube

I lost more than 3 hours to solve the matter but IN VAIN!!!

I made an example, here is it

        String mainstring = "when pigs fly";
        MessageBox.Show(mainstring); // Output: when pigs fly
        mainstring = mainstring.Split(new[] { "when", "fly" }, StringSplitOptions.None)[1];
        MessageBox.Show(mainstring); // Output: pigs

Everything was ok and the output is correct (I'm talking about the right way to use this function)

But the matter when I tried to use it again with a different project it didn't get the exact string and the output was different

Here is my code which I'm stuck on

                             string mainstring = @"{CODE_1:Enter Your Key!}
{CODE_2:vip}
{CODE_3:vip}
{CODE_4:Select Server}
{CODE_5:https://[TS]/A_API/GetAccess.php}
{CODE_6:[K]}
{CODE_7:[IP]}
{CODE_8:[PP]}
{CODE_9:[Version]}";

                    CODE_5 = mainstring.Split(new[] { "{CODE_5:", "}" }, StringSplitOptions.None)[1];

                    MessageBox.Show(CODE_5); 

The output wasn't https://[TS]/A_API/GetAccess.php the output was {CODE_3:vip

CodePudding user response:

You are using Split to split your main string on multiple different values, you could probably still retrieve the value you want, but using a different index.

Instead of trying to split your main string with both strings at once like your current code:

mainstring.Split(new[] { "{CODE_5:", "}" }, StringSplitOptions.None)[1];

try splitting it up, into two actions:

mainstring.Split(new string[] { "{CODE_5:" }, StringSplitOptions.None)[1].Split(new string[] { "}" }, StringSplitOptions.None)[0];

and see what you get.

This should first give you:

mainstring.Split(new string[] { "{CODE_5:" }, StringSplitOptions.None)[1] =

https://[TS]/A_API/GetAccess.php} {CODE_6:[K]} {CODE_7:[IP]} {CODE_8:[PP]} {CODE_9:[Version]}

which can then be split on "}", which would give us:

mainstring.Split(new string[] { "{CODE_5:" }, StringSplitOptions.None)[1].Split(new string[] { "}" }, StringSplitOptions.None)[0] =

https://[TS]/A_API/GetAccess.php

CodePudding user response:

I would like to Thank this guy @ibrennan208 because he told me what was my mistake then he shares the correct code

  CODE_5 = r2.Split(new[] { "{CODE_5:" }, StringSplitOptions.None)[1].Split(new[] { "}" }, StringSplitOptions.None)[0];
  •  Tags:  
  • c#
  • Related