Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find the position of substring in PowerShell after position x?

Tags:

powershell

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.

like image 746
user310291 Avatar asked Jul 27 '15 21:07

user310291


People also ask

How do I get the string after a specific character in PowerShell?

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.

How do I fetch a substring in PowerShell?

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 .

How do I find a specific text in PowerShell?

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.

What is IndexOf in PowerShell?

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.


1 Answers

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.

like image 56
Mathias R. Jessen Avatar answered Oct 14 '22 23:10

Mathias R. Jessen