Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
0 like 0 dislike
917 views
in PowerShell by 66 69 85

How can I extract a substring from a Text using PowerShell?

I have the below string:

--hello--

I want to extract just specific text from the full text as below

hello

what is the best way to perform substring in PowerShell?


1 Answer

0 like 0 dislike
by 66 69 85
 
Best answer

What's the substring function in PowerShell?

The substring() method is one of the best ways of extracting characters between two indices (positions).

How to use substring() in PowerShell?

To use substring() in PowerShell, you should provide the below parameters:

  1. Start: required, start position
  2. End: Optional, end position (up to, but not including). If not omitted: the rest of the string.

Substring PowerShell Example

$txt= "--hello-"
$txt.Substring(2,5)
$txt.Substring(1)

Output:

hello
-hello-
If you don’t ask, the answer is always NO!
...