0

I just came across something I would ask you for other possible solutions. I have a string:

string text = "This is a very serious sample text, not a joke!"

Now I would like to find the position of the word "serious" and get the rest of the string AFTER "serious".

One way I would solve this is:

$text="This is a very serious sample text, not a joke!"
$start=($text).IndexOf("serious")


(($text).Substring($start+"serious".Length)).TrimStart()

I am sure there is a regex solution for this as well, but I was wondering if I can use IndexOf() and then Substring to get the rest of the string AFTER "serious".

I was also looking into this post here: Annoying String Substring & IndexOf but either it is not the solution/question I am looking for or I didnt understand...

Thanks for your help in advance, Adis

Adis1102
  • 192
  • 1
  • 11

1 Answers1

1

Since one of SubString's overloads takes only the starting index, first find where serious (note the trailing space) is and then pick substring from that point plus length of what was searched for.

By putting the search term into a variable, one can access its length as a property. Changing the search term would be easy too, as it requires just updating the variable value instead of doing search and replace for string values.

Like so,

$searchTerm = "serious " 
$start = $text.IndexOf($searchTerm)
$text.Substring($start + $searchTerm.length)
sample text, not a joke!

As for a simple regex, use -replace and pattern ^.*serious . That would match begin of string ^ then anything .* followed by seroius . Replacing that with an empty string removes the matched start of string. Like so,

"This is a very serious sample text, not a joke!" -replace '^.*serious ', ''
sample text, not a joke!

There might be cases in which Extension Methods would be straight-forward solution. Those allow adding new methods to existing .Net classes. The usual solution would be inheriting, but since string is sealed, that's not allowed. So, extension methods are the way to go. One case could be creating a method, say, IndexEndOf that'll return where search term ends.

Adding .Net code (C# in this case) is easy enough. Sample code is adapted from another answer. The IndexEndOf method does the arithmetic and returns index where the pattern ended at. Like so,

$code=@'
public class ExtendedString {

    public string s_ {get; set;}

    public ExtendedString(string theString){
        s_ = theString;
    }

    public int IndexEndOf(string pattern)
    {
        return s_.IndexOf(pattern) + pattern.Length;
    }

    public static implicit operator ExtendedString(string value){
        return new ExtendedString(value);
    }
}
'@
add-type -TypeDefinition $code

$text = "This is a very serious sample text, not a joke!"
$searchTerm = "serious "
$text.Substring(([ExtendedString]$text).IndexEndOf($searchTerm)) 

sample text, not a joke!
vonPryz
  • 22,996
  • 7
  • 54
  • 65
  • Thanks for your answer, putting the search term to a variable is a good idea and clean way, I agree on that. However, the rest is pretty much the same I already gave as an answer ..my question was, is there another way to do that, still using IndexOf and no regex. – Adis1102 Sep 23 '20 at 07:01
  • @Adis1102 Well, you can do `$start = $text.IndexOf($searchTerm) + $searchTerm.Length` and then call substring, but still you need to calculate somehow the point from substring is going to start. Is there a practical problem you are trying to solve? Explain that on the question itself, then maybe better answers might be proposed. – vonPryz Sep 23 '20 at 07:07
  • Thanks for the regex! I am trying to understand using regex and thanksful whenever I can learn something new. There is actually no problem I want to solve, or at least I solved it with my way shown in the question. IndexOf finds always the beginning of the desired string, int or whatever. I thought maybe there is a way to find the end of a desired string. I know there are modifications of IndexOf like LastIndexOf and others... – Adis1102 Sep 23 '20 at 07:35
  • 1
    @Adis1102 An extension method can be used to create a method that does whatever is needed - without subclassing. See the new sample. – vonPryz Sep 23 '20 at 08:12