# Removing whitespaces in a Path (string) between '/' and words.

**URL:** <https://forums.powershell.org/t/removing-whitespaces-in-a-path-string-between-and-words/15550>\
**Category:** PowerShell Help\
**Created:** [January 28, 2021, 9:35pm UTC](https://forums.powershell.org/t/removing-whitespaces-in-a-path-string-between-and-words/15550 "2021-01-28T21:35:19Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bjorn-roalkvam](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/bjorn-roalkvam/32/13_2.png) [@bjorn-roalkvam](https://forums.powershell.org/u/bjorn-roalkvam)\
**Post date:** [January 28, 2021, 9:35pm UTC](https://forums.powershell.org/t/removing-whitespaces-in-a-path-string-between-and-words/15550/1 "2021-01-28T21:35:19Z")

</div>

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”

&nbsp;

Current:  
$Path2 = “Home delivery/November/Case13 /Orders”

Wanted:  
$Path2 = “Home delivery/November/Case13/Orders”

&nbsp;

Current:  
$Path3 = “msc IT/Test/1 /Case13 /Orders”

Wanted:  
$Path3 = “msc IT/Test/1/Case13/Orders”

&nbsp;

Any tips:-) ?

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [January 28, 2021, 9:43pm UTC](https://forums.powershell.org/t/removing-whitespaces-in-a-path-string-between-and-words/15550/2 "2021-01-28T21:43:40Z")

</div>

How about **.trimStart()** and **.trimEnd()**? You could even combine them … try it!

```
"'$(' White space inside and outside of a string '.TrimStart().TrimEnd())'"
```

---

<div class="post-metadata">

**Author:** ![bjorn-roalkvam](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/bjorn-roalkvam/32/13_2.png) [@bjorn-roalkvam](https://forums.powershell.org/u/bjorn-roalkvam)\
**Post date:** [January 28, 2021, 10:30pm UTC](https://forums.powershell.org/t/removing-whitespaces-in-a-path-string-between-and-words/15550/3 "2021-01-28T22:30:31Z")

</div>

Hi Olaf

Thank you for the fast reply.

Not sure how I can use TrimStart() and TrimEnd() so that only target whitespaces are removed.

&nbsp;

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]}
```

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [January 28, 2021, 10:44pm UTC](https://forums.powershell.org/t/removing-whitespaces-in-a-path-string-between-and-words/15550/4 "2021-01-28T22:44:58Z")

</div>

Use a regex to search for whitespace with a lookahead for the ‘/’

&nbsp;

```
'Home delivery/October /Case1' -replace '\s+(?=\/)'
Home delivery/October/Case1
```

---

<div class="post-metadata">

**Author:** ![bjorn-roalkvam](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/bjorn-roalkvam/32/13_2.png) [@bjorn-roalkvam](https://forums.powershell.org/u/bjorn-roalkvam)\
**Post date:** [January 28, 2021, 10:48pm UTC](https://forums.powershell.org/t/removing-whitespaces-in-a-path-string-between-and-words/15550/5 "2021-01-28T22:48:06Z")

</div>

> [@](#):
>
> 2 ‘Home delivery/October /Case1’ -replace ‘\s+(?=/)’ Home delivery/October/Case1

Excellent thank you, that is much shorter!

I need to readup on regex.

&nbsp;

brgs

&nbsp;

Bjørn

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [January 28, 2021, 11:05pm UTC](https://forums.powershell.org/t/removing-whitespaces-in-a-path-string-between-and-words/15550/6 "2021-01-28T23:05:26Z")

</div>

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 '/'
```

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![bjorn-roalkvam](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/bjorn-roalkvam/32/13_2.png) [@bjorn-roalkvam](https://forums.powershell.org/u/bjorn-roalkvam)\
**Post date:** [January 29, 2021, 2:15am UTC](https://forums.powershell.org/t/removing-whitespaces-in-a-path-string-between-and-words/15550/7 "2021-01-29T02:15:44Z")

</div>

[quote quote=288955]Another option would be …

PowerShell

\<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\>

| 

1

2

 | 

$Path3 = "msc IT/Test/1 /Case13 /Orders"

($Path3 -split '/' | ForEach-Object { $\_.TrimEnd().TrimStart() } ) -join '/'

 |

… or this way …

PowerShell

\<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\>

| 

1

2

 | 

$Path3 = "msc IT/Test/1 /Case13 /Orders"

$Path3.Split('/').ForEach({$\_.TrimEnd().TrimStart()}) -join '/'

 |

[/quote] You make it seem so easy :D

&nbsp;

Good stuff!

&nbsp;

brgs

&nbsp;

Bjørn

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [January 29, 2021, 2:49am UTC](https://forums.powershell.org/t/removing-whitespaces-in-a-path-string-between-and-words/15550/8 "2021-01-29T02:49:55Z")

</div>

> [@](#):
>
> You make it seem so easy 😀

That’s not me - that’s Powershell. 😉

But thanks anyway.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:27pm UTC](https://forums.powershell.org/t/removing-whitespaces-in-a-path-string-between-and-words/15550/9 "2024-05-16T20:27:59Z")

</div>


