*** WARNING! ***
*** This Blog Post will talk about Developer Terms ***
*** ItPros are cautioned to break out their (Patent Pending) ***
*** “Peril Sensitive Sunglasses” (Ala the HitchHikers’ Guide to the Galaxy) ***
*** Before Reading further. If you are amongst the Bold and the DARING and the ***
*** Truly Devil May Care of ITPros, Please feel free to Read on and live Daring! ***
But don’t say I didn’t warn you 🙂
Sean – The Energized Tech
Today my fellow ITPros, we are going to step OUT of our element and do something SO scary, you must just throw your Fluke meters away in Disgust.
Yes, we’re going to use a METHOD!
Over the next while, we’re going to learn a LOT of “Methods”, and ye Lords of COBOL willing; you will ENJOY it.
Funny words aside, we’re going to little by little show you some of the Methods available in Powershell and perhaps get you a little bit used to digging into the land that Developers live in.
Today’s Method is brought to you by the letter “S”. It is “substring()”
Substring() is a function if you would that lets you pull out pieces of a Cha……
“Zzzzzzzzz Gzaaawwwww..Snort snort..”
“You in the back, WAKE UP!” *WHAP*
I can always spot a snoozing ItPro when I’m talking out of their scope. So let’s keep this simple.
You have a String, Text information. You need a PIECE of it. Substring() is one of the Methods available to you to do just that. Nice and simple as well.
If assign a small word say…
To a variable and run that through a GET-MEMBER I get
These all all the Members of object $Word. As you can see we have a Method call Substring(). To access a Method is almost just like accessing a Property. Simply typing a “.” between the Object “$Word” and the name of the Method you want to apply to the Object “SubString()”
So we just type in…
And yaaayyy it just….
It just immediately gave us a big Wawa error
That’s because many methods require additional parameters, information to know what you WANT them to do. Some don’t.
To get an Indication what your Method “Substring()” wants you can have Powershell TELL you. The very same GET-MEMBER we ran before can be used to just tell us about “Substring()” (or any other Method for that matter)
Just add on –Name and the name of Method after a ‘GET-MEMBER’, then pipe it through FORMAT-LIST to see all the details. Look for “Definition”
What this definition translates to is it wants the parameter in one of two ways.
The first one
string Substring(int startIndex)
Says “I want you to put an INTeger in here (a number) saying WHERE to start”
So if we do
(remembering our Word was ’SuperCaliFragilIsticExpiAliDocious’) we will get the Variable $Word starting at position 10 of the character array and all the rest)
Will be what we see
and if you do a
Your results will be
Since it starts in by 25 characters. And now it gets fun. The OTHER definition of substring() was
string Substring(int startIndex, int length)
This translates to Says “I want you to put an INTeger in here (a number) saying WHERE to start, then I want another INTeger (num……
Excuse me for a minute.
…Ahem, nobody sleeps in my Blog.
So as I was saying This translates to Says “I want you to put an INTeger in here (a number) saying WHERE to start, then I want another INTeger (number again) saying how MUCH to show from that point forward.
So with the name $Word we can now do this
will show us
Since that part of the string is 5(Five) letters in and we’re showing 4(Four) from that position forward. Or I can do a
Which will show us
Because again, it’s going in 24(Twenty-Four) characters, and displaying the next 6(Six)
The thing is, once you understand a LITTLE about one Method, it’s not TOO horribly difficult to pick up on some of the rest.
You may now return to your regularly scheduled channel and Remove the Peril Sensitive Sunglasses. And Sleepy from the back can go back to work. 🙂
The Energized Tech