# Powershell Split wtih multi-character delimiter

**URL:** <https://forums.powershell.org/t/powershell-split-wtih-multi-character-delimiter/23255>\
**Category:** PowerShell Help\
**Created:** [December 5, 2023, 2:57pm UTC](https://forums.powershell.org/t/powershell-split-wtih-multi-character-delimiter/23255 "2023-12-05T14:57:11Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![littlewing1977](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/littlewing1977/32/21_2.png) [@littlewing1977](https://forums.powershell.org/u/littlewing1977)\
**Post date:** [December 5, 2023, 2:57pm UTC](https://forums.powershell.org/t/powershell-split-wtih-multi-character-delimiter/23255/1 "2023-12-05T14:57:11Z")

</div>

I am trying to split a string using “.^.” as the delimiter. I have been unable to get it to behave the way I expect it to. For clarification, each segment is separated by “.^.” I don’t want it to split on each character separately. Right now, I’m using something like this.

> $str=“yellow.^…^.C.^.5/10/2023 ReissuedSA. CSM approval 3/13/2020 ticket836822. user must run the Authorization Manager application that generates a unique reference code based on the hardware of the machine on which the software has been installed. .^…^.CSM approval 3/13/2020 ticket836822.^.”  
> $arr=$str.split(“.^.”)

I have tried using $str -split “.^.”, as some posts suggest, but returns a single string without splitting it at all.  
I have also read that this may be expecting a RegEx, but I was unable to get that to work either.  
If I split with just the caret, it gets closer to the results I’m expecting, but it leaved the periods around each field.

I am working with an existing database also. So I can’t use a different delimiter.

Any help is much appreciated.

---

<div class="post-metadata">

**Author:** ![neemobeer](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/neemobeer/32/4087_2.png) [@neemobeer](https://forums.powershell.org/u/neemobeer)\
**Post date:** [December 5, 2023, 3:10pm UTC](https://forums.powershell.org/t/powershell-split-wtih-multi-character-delimiter/23255/2 "2023-12-05T15:10:54Z")

</div>

Works fine for me other than the fact that the “…” keep getting encoded as a single character. Had to replace those with actual “…”

---

<div class="post-metadata">

**Author:** ![littlewing1977](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/littlewing1977/32/21_2.png) [@littlewing1977](https://forums.powershell.org/u/littlewing1977)\
**Post date:** [December 5, 2023, 3:12pm UTC](https://forums.powershell.org/t/powershell-split-wtih-multi-character-delimiter/23255/3 "2023-12-05T15:12:21Z")

</div>

I’m getting over 20 elements in the array. It’s splitting on the periods in the text fields.

---

<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:** [December 5, 2023, 3:29pm UTC](https://forums.powershell.org/t/powershell-split-wtih-multi-character-delimiter/23255/4 "2023-12-05T15:29:45Z")

</div>

Try

```auto
$arr = $str -split "\.\^\."

```

And BTW: Because you formatted your code as quote and not as code the code got messed up - especially your input string. ☝🏼 ☝🏼 ☝🏼

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( \</\> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

[How to format code in PowerShell.org](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/b/bee50c628238f2c076c0504efb6b15f2fb11f002.gif) 1 \<---- Click 👆🏽 😉

( !! Sometimes the _preformatted text_ button hides behind the settings gear symbol. 😉 )

… just to show the difference:

```auto
$str = "yellow.^..^.C.^.5/10/2023 ReissuedSA. CSM approval 3/13/2020 ticket836822. user must run the Authorization Manager application that generates a unique reference code based on the hardware of the machine on which the software has been installed. .^..^.CSM approval 3/13/2020 ticket836822.^."
$arr = $str -split "\.\^\."
$arr.count

```

The output is “`7`” 😏

---

<div class="post-metadata">

**Author:** ![littlewing1977](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/littlewing1977/32/21_2.png) [@littlewing1977](https://forums.powershell.org/u/littlewing1977)\
**Post date:** [December 5, 2023, 3:44pm UTC](https://forums.powershell.org/t/powershell-split-wtih-multi-character-delimiter/23255/5 "2023-12-05T15:44:40Z")

</div>

Thanks. That worked. I tried it with the backslashes before and it didn’t work. I think I used that with the .split method.

---

<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:** [December 5, 2023, 3:46pm UTC](https://forums.powershell.org/t/powershell-split-wtih-multi-character-delimiter/23255/6 "2023-12-05T15:46:50Z")

</div>

Actually

```auto
$str.Split('.^.')

```

works for me as well. The result is the same. 🤷🏼‍♂️

---

<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:** [December 5, 2023, 5:44pm UTC](https://forums.powershell.org/t/powershell-split-wtih-multi-character-delimiter/23255/7 "2023-12-05T17:44:21Z")

</div>

One thing I’ll point out using the split method is something that changed between PS5 and 7.

At one point in time I was going to blog but I guess I keep forgetting to do it, but ironically enough, I wrote a blog on this: [Using .NET 5 split Method to split strings in PowerShell 7.1+ (dotnvo.github.io)](https://dotnvo.github.io/2021-06-10-String-Method-in-Powershell-7/)

If you run your same command in PS5, it won’t return 7 objects, it’ll return 22.

TLDR: That’s because in PS5, it splits it on each character by default in PowerShell. .NET 4 the string class only had methods that took characters as parameter types IIRC. So PowerShell does its magic on the backend. .NET 5 changed the game a bit, and so PS7 also now behaves differently, specifically you can give it a string to split on. To illustrate it a bit easier:

PS7:

```powershell
'S.t^r.i.n^g'.Split('.^.')

```

OUTPUT:

```plaintext
S.t^r.i.n^g

```

PS5:

```powershell
'S.t^r.i.n^g'.Split('.^.')

```

OUTPUT

```plaintext
S
t
r
i
n
g

```

So PS7, it takes that whole thing and splits it as a string, and since it can’t find any matches, it outputs it the same way.

PS5, it seems the same exact command but because there’s no way to split on a string, it knows its splitting on each individual character, so it does just that.

---

<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:** [December 6, 2023, 5:40am UTC](https://forums.powershell.org/t/powershell-split-wtih-multi-character-delimiter/23255/8 "2023-12-06T05:40:18Z")

</div>

> [@dotnVo](#):
>
> If you run your same command in PS5, it won’t return 7 objects, it’ll return 22.

So it is preferable to stick with the pure PowerShell syntax for your code since you get consistent results in both versions. 🤷🏼‍♂️ 👍🏼 🤟🏼

---

<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:** [December 6, 2023, 5:45am UTC](https://forums.powershell.org/t/powershell-split-wtih-multi-character-delimiter/23255/9 "2023-12-06T05:45:24Z")

</div>

For this probably. I was simply providing additional context if OP was seeing different results it could be because of PS version with the split method 🙂

---

<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:13pm UTC](https://forums.powershell.org/t/powershell-split-wtih-multi-character-delimiter/23255/10 "2024-05-16T20:13:38Z")

</div>

Closed on May 16, 2024, 8:13pm UTC


