Windows PowerShell Tip on this link is an excellent article
on how to manipulate strings on PowerShell.
Using the analogy from the above link, we are able to tweak
the code to convert first character to upper case.
Below is a simple PowerShell code that converts first
character on a string specified using PowerShell.
Test the PowerShell code below; Using PowerShell ISE, a
friendly GUI for quick testing of PowerShell code snippet.
Here's the code below, codes are commented on each line. It
describes what should be the expected desired output for each line.
============================
$string_var = "my Rock and My Hope"
$anotherString_var = $string_var
#code below will get the first character
$string_var = $string_var.Substring(0,1)
#code below will get the whole string except the first
character
$anotherString_var = $anotherString_var.Substring(1)
#code below will convert the first letter to upper case
#which is stored on $string_var
$finalString_var = $string_var.ToUpper()
#finally combined the two strings
write-host $finalString_var$anotherString_var
============================
Output string is: My Rock and My Hope
Output string is: My Rock and My Hope
Above script will only change to upper case the first letter.
If need to change to upper case the letters on a string separated by space, please check code snippet below:
============================
$Dstring = "the taller the bamboo grows the lower it bends"
$Darray = $Dstring -split " "
$xUpper = $Darray.Substring(0,1).ToUpper()
$xStringSub=$Darray.Substring(1)
$xfinal=""
$xcount = $Darray.count -1
#write-host $xcount
for ($i=0; $i –le $xcount; $i++)
{
$xspace=" "
$yxUpper=$xUpper[$i]
$dxStringSub=$xStringSub[$i]
$xfinal = $xfinal+$xspace+$yxUpper+$dxStringSub
}
write-host $xfinal
============================
Output string is: The Taller The Bamboo Grows The Lower It Bends
Check out this Technet link to dig more on how to use array in PowerShell:
http://technet.microsoft.com/en-us/library/hh847882.aspx
Hope it helps..Cheers!!! It's a quick and easy way to convert first character to uppercase using PowerShell.
----------------------------------
Free Android app, download at Google play:
Android Divine Mercy Chaplet Guide
https://play.google.com/store/apps/details?id=com.dmercyapp
----------------------------------
Free Android app, download at Google play:
Android Divine Mercy Chaplet Guide
https://play.google.com/store/apps/details?id=com.dmercyapp
Comments
Post a Comment