-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSplit-StringOnLiteralString.ps1
126 lines (122 loc) · 5.32 KB
/
Split-StringOnLiteralString.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
function Split-StringOnLiteralString {
# .SYNOPSIS
# Splits a string into an array using a literal string as the splitter.
#
# .DESCRIPTION
# Splits a string using a literal string (as opposed to regex). The
# function is designed to be backward-compatible with all versions of
# PowerShell and has been tested successfully on PowerShell v1. This
# function behaves more like VBScript's Split() function than other
# string splitting-approaches in PowerShell while avoiding the use of
# RegEx.
#
# .PARAMETER StringToSplit
# This parameter is required; it is the string to be split into an
# array.
#
# .PARAMETER Splitter
# This parameter is required; it is the string that will be used to
# split the string specified in the StringToSplit parameter.
#
# .EXAMPLE
# $result = Split-StringOnLiteralString -StringToSplit 'What do you think of this function?' -Splitter ' '
# # $result.Count is 7
# # $result[2] is 'you'
#
# .EXAMPLE
# $result = Split-StringOnLiteralString 'What do you think of this function?' ' '
# # $result.Count is 7
#
# .EXAMPLE
# $result = Split-StringOnLiteralString -StringToSplit 'foo' -Splitter ' '
# # $result.GetType().FullName is System.Object[]
# # $result.Count is 1
#
# .EXAMPLE
# $result = Split-StringOnLiteralString -StringToSplit 'foo' -Splitter ''
# # $result.GetType().FullName is System.Object[]
# # $result.Count is 5 because of how .NET handles a split using an
# # empty string:
# # $result[0] is ''
# # $result[1] is 'f'
# # $result[2] is 'o'
# # $result[3] is 'o'
# # $result[4] is ''
#
# .INPUTS
# None. You can't pipe objects to Split-StringOnLiteralString.
#
# .OUTPUTS
# System.String[]. Split-StringOnLiteralString returns an array of
# strings, with each string being an element of the resulting array
# from the split operation. This function always returns an array, even
# when there is zero elements or one element in it.
#
# .NOTES
# This function also supports the use of positional parameters instead
# of named parameters. If positional parameters are used instead of
# named parameters, then two positional parameters are required:
#
# The first positional parameter is the string to be split into an
# array.
#
# The second positional parameter is the string that will be used to
# split the string specified in the first positional parameter.
#
# Also, please note that if -StringToSplit (or the first positional
# parameter) is $null, then the function will return an array with one
# element, which is an empty string. This is because the function
# converts $null to an empty string before splitting the string.
#
# Version: 3.0.20250211.1
#region License ####################################################
# Copyright (c) 2025 Frank Lesniak
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#endregion License ####################################################
param (
[string]$StringToSplit = '',
[string]$Splitter = ''
)
$strSplitterInRegEx = [regex]::Escape($Splitter)
$result = @([regex]::Split($StringToSplit, $strSplitterInRegEx))
# The following code forces the function to return an array, always,
# even when there are zero or one elements in the array
$intElementCount = 1
if ($null -ne $result) {
if ($result.GetType().FullName.Contains('[]')) {
if (($result.Count -ge 2) -or ($result.Count -eq 0)) {
$intElementCount = $result.Count
}
}
}
$strLowercaseFunctionName = $MyInvocation.InvocationName.ToLower()
$boolArrayEncapsulation = $MyInvocation.Line.ToLower().Contains('@(' + $strLowercaseFunctionName + ')') -or $MyInvocation.Line.ToLower().Contains('@(' + $strLowercaseFunctionName + ' ')
if ($boolArrayEncapsulation) {
return ($result)
} elseif ($intElementCount -eq 0) {
return (, @())
} elseif ($intElementCount -eq 1) {
return (, (, $StringToSplit))
} else {
return ($result)
}
}