techthoughts2
10/23/2015 - 7:45 PM

String Manipulation

String Manipulation

#replace
if($vDisk -contains " "){
   $vDisk = $vDisk -replace(" ",",")
}


#remove last character
$string.Substring(0,$string.Length-1)

#extract a specific string set from a long string
[regex]$r = "(?<=Vlan).*?(?= )"
$r.matches($nativeVLANString) | `
ForEach-Object {
	$nativeVLANStringFinal = $_.Value
}

#match airport code
if ($APC -match "^[A-Z]{3}$") {
 #correct 
}

#splitting a string up at a certain character
$split = $switch.display_name.split("[")
$switchname = $split[0] + ".$region"

#another example
$config.Hostname = ($corePull.result.name.split("."))[0]

#reduce string to a certain length
if($compName.length -gt 6){
    $compName = $compName.substring(0,6)
}

#get all the object things and put them into a list serparated by commas
$secondaryDisks = $t.'VM-Object'.DataDisks
($secondaryDisks | Select-Object -ExpandProperty DiskSize) -join ","

# get last character of a string
$a = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
$a.substring($a.length - 1)