Feature Request: Add .slice() method for strings and arrays
See original GitHub issueSummary of the new feature/enhancement
Add a .slice() method for strings and arrays similar to that of JavaScript. See: https://www.w3schools.com/jsref/jsref_slice_array.asp
For strings, this would provide a method to return the rightmost N characters of a string by using negative value. For example:
# Rightmost N existing solution
PS C:\ps1> $text = "ABCDEFG"
PS C:\ps1> $text.Substring($text.Length - 2)
FG
# Rightmost N characters with slice()
PS C:\ps1> "ABCDEFG".slice(-2)
FG
# Padding a month number with slice():
PS C:\ps1> ('0' + (get-Date).Month).slice(-2)
07
Below are a few additional examples of how slice() might work:
PS C:\ps1> "007".slice(-2)
07
PS C:\ps1> "ABCDEFG".slice(3)
DEFG
PS C:\ps1> $fruits = @("Banana", "Orange", "Lemon", "Apple", "Mango")
PS C:\ps1> $fruits.slice(2)
Lemon
PS C:\ps1> $fruits.slice(2..4)
Lemon
Apple
Mango
PS C:\ps1> $fruits.slice(-2)
Apple
PS C:\ps1> $fruits.slice(-2..-1)
Apple
Mango
Proposed technical implementation details (optional)
Virtually all of the slice() functionality can be leveraged from the existing array code. The PowerShell code below serves as a simple example of how to define slice():
# String slice()
$stringMethod = @{
MemberName = 'slice';
MemberType = 'ScriptMethod';
Value = { param([int32] $t) $( if ($t -ge 0) {$this.substring($t)} else {$this.substring(($this.Length + $t))}) }
Force = $true
}
Update-TypeData -TypeName 'string' @stringMethod
# System.Array slice()
$arrayMethod = @{
MemberName = 'slice';
MemberType = 'ScriptMethod';
Value = { param($t) $this[$t] }
Force = $true
}
Update-TypeData -TypeName 'System.Array' @arrayMethod
This method is a convenience feature for those familiar with JavaScript and to provide a way to easily extract the rightmost characters of a string (the substring() method does not permit negative values).
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
4 Ways to Use JavaScript .slice() Method and How to Do ...
The method can be used to quickly and easily extract a portion of an array or string. This function can be very helpful...
Read more >Adding a slice function to a string - javascript
The slice method can be used for an Array or a String variable. The intention of slice method is to slice out some...
Read more >How does the slice function work in JavaScript?
The slice function in JavaScript is used to extract a portion of an array and returns it as a new array without modifying...
Read more >JavaScript Array slice() Method
The slice() method returns selected elements in an array, as a new array. The slice() method selects from a given start, up to...
Read more >Let's clear up the confusion around the slice( ), splice( ), & ...
The slice( ) method copies a given part of an array and returns that copied part as a new array. It doesn't change...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is probably something that should not be done in PowerShell. Instead, file this issue against .Net itself.
You miss the point. If this change is made in .net, it would just appear in PowerShell as if by magic. A new build targeted at an updated .net would just work with no effort by the team. Asking for it inside powershwll is just more work to adapt the string class. In my view, .net is the right place for such a method. Mileage will vary.