Is there a simple way to do so; I can't find any. Am I obliged to scan character by character?
I don't want a string as return, but a position so do not suggest SubString.
To get PowerShell substring after a character in the given string, use the IndexOf method over the string to get the position of the character. Use the position in PowerShell substring as the start index to get a substring. PowerShell substring() method returns the part of the string.
To find a string inside of a string with PowerShell, you can use the Substring() method. This method is found on every string object in PowerShell. The first argument to pass to the Substring() method is the position of the leftmost character. In this case, the leftmost character is T .
You can use Select-String similar to grep in UNIX or findstr.exe in Windows. Select-String is based on lines of text. By default, Select-String finds the first match in each line and, for each match, it displays the file name, line number, and all text in the line containing the match.
The IndexOf and LastIndexOf may be used to locate a character or string within a string. IndexOf finds the first occurrence of a string, LastIndexOf finds the last occurrence of the string. In both cases, the zero-based index of the start of the string is returned.
Yes, you can. The IndexOf()
method has an overload that takes a startIndex
argument:
PS C:\> "WordsWordsWords".IndexOf("Words")
0
PS C:\> "WordsWordsWords".IndexOf("Words", 2)
5
In the second example, we look for the index of the substring "Words" starting after character index 2.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With