0

I need to get 2 substrings S1 and S2 before and after @@token@@ from the following string

Token = 15d7b736-d8f0-e711-b842-100ba9d19b04@@token@@131599776034855065

'/// To Get S1
S1 = Token.Substring(0, Token.IndexOf("@@token@@"))

S1 will be '15d7b736-d8f0-e711-b842-100ba9d19b04'

but I'm not sure how to get S2? S2 should be '131599776034855065'

Any help :)

Geo Concepts
  • 177
  • 3
  • 13
  • `Token.IndexOf("foo") + "foo".Length`? – CodeCaster Jan 09 '18 at 13:31
  • Possible duplicate of [Need to get a string after a "word" in a string in c#](https://stackoverflow.com/questions/14998595/need-to-get-a-string-after-a-word-in-a-string-in-c-sharp) – CodeCaster Jan 09 '18 at 13:31

2 Answers2

0

Try:

string s = "15d7b736-d8f0-e711-b842-100ba9d19b04@@token@@131599776034855065";
string str1 = s.Substring(0, s.IndexOf("@"));
string str2 = s.Substring(s.LastIndexOf("@")+1, s.Length -1 - s.LastIndexOf("@"));

Or another way is splitting the string based on @@token@@:

string[] strArr = s.Split(new string[] { "@@token@@" }, StringSplitOptions.None); 

str[0]="15d7b736-d8f0-e711-b842-100ba9d19b04"

And

str[1]="131599776034855065"

Note:

To split string based string you should use split string array overload.

Aria
  • 3,724
  • 1
  • 20
  • 51
0

Use the lastIndexOf() method to get.

string Token = "15d7b736-d8f0-e711-b842-100ba9d19b04@@token@@131599776034855065";
string delimiter = "@@token@@";
string S1 = Token.Substring(0, Token.IndexOf(delimiter));
string S2 = Token.Substring(Token.LastIndexOf(delimiter) + delimiter.Length, Token.Length - delimiter.Length - S1.Length );
Console.WriteLine(S1); // or print(s1)
Console.WriteLine(S2); //or print(s2)
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Sasi
  • 140
  • 1
  • 8