Substring question

Hi all,
i’m just studying a case of data manipulation, i’ve seen this 3d.

I just want to understand why this string is correct.
text.Substring((text.LastIndexOf(“Start”) +5), (text.IndexOf(“end”) - (text.LastIndexOf(“Start”) +5)))
to obtain get this text form this string
“words
Start words
Start Get This
Text
End
End”
If you can explain the logic behind the data manipulation
Thanks

Hi,

It is straightforward to understand.

Having that into account Substring(initial position,substring length)

  • LastIndexOf(“Start”) gives you the initial position of the last occurrence of “Start”, but you need the final position, therefore you add 5.
  • IndexOf(“end”) gives you the initial posistion of the first occurence of “end”. With the operation you are calculating the length of the substring you are looking for.
1 Like

Thanks for reply.
But i can’t understand the second part, the operation.

  1. (text.LastIndexOf(“Start”) +5) = Get This Text End End
  2. (text.IndexOf(“End”) = End End
  3. ?
    What i’m missing?

the final part is substracting the position of the first End by the position of the first Start +5 in order to get only the “Get This text”

1 Like

Fine:

  • text.LastIndexOf(“Start”) +5 = 16 (just a guess)
  • Substring(text.LastIndexOf(“Start”) +5) = Substring(16) = Get This Text End End. Because if you don’t specify the length, it is taking up to the end.
  • text.IndexOf(“End”) = 30 (other guess)
  • text.IndexOf(“end”) - ((text.LastIndexOf(“Start”) +5) = 30 - 16 = 14
  • Substring((text.LastIndexOf(“Start”) +5), (text.IndexOf(“end”) - (text.LastIndexOf(“Start”) +5))) = Substring(16,14) = Get This Text (with a white space at the end)
2 Likes

Thanks for yout explanation, now is clear :grinning:

Last question, can you handle it with “Lenght -10” for example… Instead of calculating index without operation?
Thx

You can do that operation, if that gives you the result you want, go ahead.

The best is trying when you are not sure.

1 Like

Thx, i’ll try and now i really understand that…

Only one more question, indexof and lastindexof is for search the first and the last word…

For ex. End by end by end how can i identify the second END? THX

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.