seyo IV
seyo IV

Reputation: 177

Place second part of a string at the first place

I have a string "givenname surname". What I want is to turn this "surname, givenname". First I thought I would need to somehow place the surname at the first place, then replace the empty space with a comma and space.

I do not have any code yet :C sry.

Upvotes: 0

Views: 42

Answers (1)

Nirav Mistry
Nirav Mistry

Reputation: 989

Agreed on @ansgar wiechers's comment.

$str = "Firstname Lastname"
$str -replace '(\w+)\s+(\w+)','$2, $1'

Output :

Lastname, Firstname

Upvotes: 1

Related Questions