0

I have the following string:

Account name:            abc.def

where abc.def is username, John.doe for example.
I need to trim this string and leave only the username, example abc.def

Whatever I try with select-string or -match doesn't get me there.

1
  • 1
    From where are you receiving this string? Maybe there's an easier way than fiddling with strings. Commented May 11, 2021 at 15:07

2 Answers 2

3

You could remove everything up to and including the : with -replace, then let String.Trim() take care of the whitespace:

PS ~> $string = "Account name:            abc.def"
PS ~> ($string -replace '^.*?:').Trim()
abc.def
Sign up to request clarification or add additional context in comments.

Comments

2

You can use regular expressions. For example:

$string = "Account name:            abc.def    " 

$string -match '(?<fullName>(?<firstName>[[a-z]+)\.(?<lastName>[a-z]+))'

You can then access the information using the $matches object:

$matches

Name                           Value
----                           -----
fullName                       abc.def
lastName                       def
firstName                      abc
0                              abc.def

Note: The regex here also splits the full name into the first and last names in case you need those, but if not you can just ignore that output, or change the regex to: '(?<fullName>[a-z]+\.[a-z]+)'

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.