Hi
Using Trim() clears all white spaces:(
I wonder if regex is needed since it concerns removing spaces between a special char.
Current:
$Path1 = “Home delivery/October /Case1”
Wanted:
$Path1 = “Home delivery/October/Case1”
Current:
$Path2 = “Home delivery/November/Case13 /Orders”
Wanted:
$Path2 = “Home delivery/November/Case13/Orders”
Current:
$Path3 = “msc IT/Test/1 /Case13 /Orders”
Wanted:
$Path3 = “msc IT/Test/1/Case13/Orders”
Any tips:-) ?
Olaf
2
How about .trimStart() and .trimEnd()? You could even combine them … try it!
"'$(' White space inside and outside of a string '.TrimStart().TrimEnd())'"
Hi Olaf
Thank you for the fast reply.
Not sure how I can use TrimStart() and TrimEnd() so that only target whitespaces are removed.
Guess I can do something like this also:
# Target Path
$Path3 = "msc IT/Test/1 /Case13 /Orders"
# Create Arrays
$CleanArray = @()
$SplitArray = @()
# Splity on '/' and trim away white spaces
$SplitArray += $Path3.split("/")
Foreach ($item in $SplitArray)
{
$CleanArray += $item.trim()
}
# Clear Vars
if($Count){Remove-Variable -name Count}
if($NewPath){Remove-Variable -name NewPath}
# Remove empty elements
$CleanArray = $CleanArray | Where-Object {$_}
# Get element count
$Count = $CleanArray.count
# Rebuild path
if($Count -eq 1){$NewPath = $CleanArray[0]}
if($Count -eq 2){$NewPath = $CleanArray[0]+"/"+$CleanArray[1]}
if($Count -eq 3){$NewPath = $CleanArray[0]+"/"+$CleanArray[1]+"/"+$CleanArray[2]}
if($Count -eq 4){$NewPath = $CleanArray[0]+"/"+$CleanArray[1]+"/"+$CleanArray[2]+"/"+$CleanArray[3]}
if($Count -eq 5){$NewPath = $CleanArray[0]+"/"+$CleanArray[1]+"/"+$CleanArray[2]+"/"+$CleanArray[3]+"/"+$CleanArray[4]}
if($Count -eq 6){$NewPath = $CleanArray[0]+"/"+$CleanArray[1]+"/"+$CleanArray[2]+"/"+$CleanArray[3]+"/"+$CleanArray[4]+"/"+$CleanArray[5]}
if($Count -eq 7){$NewPath = $CleanArray[0]+"/"+$CleanArray[1]+"/"+$CleanArray[2]+"/"+$CleanArray[3]+"/"+$CleanArray[4]+"/"+$CleanArray[5]+"/"+$CleanArray[6]}
if($Count -eq 8){$NewPath = $CleanArray[0]+"/"+$CleanArray[1]+"/"+$CleanArray[2]+"/"+$CleanArray[3]+"/"+$CleanArray[4]+"/"+$CleanArray[5]+"/"+$CleanArray[6]+"/"+$CleanArray[7]}
if($Count -eq 9){$NewPath = $CleanArray[0]+"/"+$CleanArray[1]+"/"+$CleanArray[2]+"/"+$CleanArray[3]+"/"+$CleanArray[4]+"/"+$CleanArray[5]+"/"+$CleanArray[6]+"/"+$CleanArray[7]+"/"+$CleanArray[8]}
if($Count -eq 10){$NewPath = $CleanArray[0]+"/"+$CleanArray[1]+"/"+$CleanArray[2]+"/"+$CleanArray[3]+"/"+$CleanArray[4]+"/"+$CleanArray[5]+"/"+$CleanArray[6]+"/"+$CleanArray[7]+"/"+$CleanArray[8]+"/"+$CleanArray[9]}
if($Count -eq 11){$NewPath = $CleanArray[0]+"/"+$CleanArray[1]+"/"+$CleanArray[2]+"/"+$CleanArray[3]+"/"+$CleanArray[4]+"/"+$CleanArray[5]+"/"+$CleanArray[6]+"/"+$CleanArray[7]+"/"+$CleanArray[8]+"/"+$CleanArray[9]+"/"+$CleanArray[10]}
Use a regex to search for whitespace with a lookahead for the ‘/’
'Home delivery/October /Case1' -replace '\s+(?=\/)'
Home delivery/October/Case1
Excellent thank you, that is much shorter!
I need to readup on regex.
brgs
Bjørn
Olaf
6
Another option would be …
$Path3 = "msc IT/Test/1 /Case13 /Orders"
($Path3 -split '/' | ForEach-Object { $_.TrimEnd().TrimStart() } ) -join '/'
... or this way ...
$Path3 = "msc IT/Test/1 /Case13 /Orders"
$Path3.Split('/').ForEach({$_.TrimEnd().TrimStart()}) -join '/'
[quote quote=288955]Another option would be …
<textarea class="urvanov-syntax-highlighter-plain print-no" style="tab-size: 4; font-size: 14px !important; line-height: 18px !important; z-index: 0; opacity: 0;" readonly="readonly" data-settings="dblclick">$Path3 = "msc IT/Test/1 /Case13 /Orders"
($Path3 -split '/' | ForEach-Object { $_.TrimEnd().TrimStart() } ) -join '/'</textarea>
|
$Path3 = "msc IT/Test/1 /Case13 /Orders"
($Path3 -split '/' | ForEach-Object { $_.TrimEnd().TrimStart() } ) -join '/'
|
… or this way …
<textarea class="urvanov-syntax-highlighter-plain print-no" style="tab-size: 4; font-size: 14px !important; line-height: 18px !important; z-index: 0; opacity: 0;" readonly="readonly" data-settings="dblclick">$Path3 = "msc IT/Test/1 /Case13 /Orders"
$Path3.Split('/').ForEach({$_.TrimEnd().TrimStart()}) -join '/'</textarea>
|
$Path3 = "msc IT/Test/1 /Case13 /Orders"
$Path3.Split('/').ForEach({$_.TrimEnd().TrimStart()}) -join '/'
|
[/quote]
You make it seem so easy :D
Good stuff!
brgs
Bjørn
Olaf
8
That’s not me - that’s Powershell.
But thanks anyway.