Skip to content

Instantly share code, notes, and snippets.

@cajones
Created April 27, 2012 10:10
Show Gist options
  • Save cajones/2508137 to your computer and use it in GitHub Desktop.
Save cajones/2508137 to your computer and use it in GitHub Desktop.
String manipulation
function Select-Head ([string]$text, [int]$index, [string]$pattern) {
$input += $text
return $input | where { $_ -is [string] } |% {
if($pattern) {
$index = $_.IndexOf($pattern)
}
if($index -ge $_.length) {return $_}
return $_.Substring(0, $index)
}
}
function Select-Tail ([string]$text, [int]$index, [int]$last, [string]$pattern) {
$input += $text
return $input | where { $_ -is [string] } |% {
if($pattern) {
$index = $_.IndexOf($pattern) + $pattern.length
}
elseif($last) {
$index = $_.length - $last
}
if($index -ge $_.length) {return}
$_.Substring($index)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment