Friday 16 October 2015

Strings in PowerShell

Like all programming languages, PowerShell uses strings very effectively and provides some interesting capabilities for a script.

PowerShell uses four types of strings.
  1. Single quoted string.
  2. Double quoted string.
  3. Single quoted Here Strings.
  4. Double quoted Here Strings.
PowerShell uses strings as sequence of 16-bit Unicode characters and is directly implemented by system.string .Net type .

Single Quoted Strings
     Single quoted string in PowerShell are nothing, But string which is represented inside single quotes.

    PS C:\ >'This is a Single Quoted string'
    This is a Single Quoted string
    PS C:\ >

    But there is some more about single quoted string,
    lets see this example,

    PS C:\ >'This is a Single quoted $var'
    This is a Single quoted $var
    PS C:\ >
    Here you can see, I have stored 'String' in a variable named $var , But single quoted string is not able to substitute the value of the $var .
    So we can use single quoted strings where we don't want to substitute a variables value in it.

    Double Quoted Strings.
      Like single quoted strings, Double quoted string are nothing , but Strings included between two double quotes .
      PS C:\ >"This is a Double Quoted string"
      This is a Double Quoted string
      PS C:\ >
      But unlike single quoted strings, Double quoted strings can substitute Values for Variables.
      PS C:\ >"This is a Double quoted $var"
      This is a Double quoted String
      PS C:\ >

      Here Strings

      First Lets have a glance on Here Strings.
      Here strings are another way of representing strings , Especially while we have large amount including multiple lines. Here strings are effectively used when generating output for another program. But the same can be achieved regular type of strings which we discussed before.
      Then Why Here Strings ?

      Lets explain it after an example,
      PS C:\ >$HereString=@"
      >>> This is
      >>> An Example
      >>> For
      >>> Here String
      >>> "@
      PS C:\ >$HereString
      This is
      An Example
      Here String
      PS C:\ >

      Here we Can see the syntax for here string.


      the <newline> is important as strings between the quotes  are treated as Here Strings and the first and last new lines are not included and the main advantage is , we can include single and double quotes regardless the type of Here Strings whether it is single Quoted or Double Quoted.

      Look here on another example,

      PS C:\ >$a='This'
      PS C:\ >$HereString=@"
      >>> $a is
      >>> An "Example"
      >>> For
      >>> 'Here String'
      >>> "@
      PS C:\ >$HereString
      This is
      An "Example"
      'Here String'
      PS C:\ >

      Here the variable is substituted for $a, Single and Double quotes are included too !!!.

      And Single Quoted Here Strings are here strings using single quotes where variables are not substituted with values - @'<newline><string><newline>'@.
      Double Quoted Here Strings are similar, But uses Double quotes instead of single quotes and the variables are substituted here.

      That's all about Strings here... Have fun Using PowerShell

      No comments:

      Post a Comment