• No results found

String Operators

In document Windows PowerShell 2.0 (Page 170-175)

Note1 = This appears to be the wrong syntax. Note2 = There is a value missing.

Note3 = Cannot connect at this time. '@

$strArray = $string | convertfrom-stringdata

The here-string defi ned in this example contains the same strings as the previous example. You then use a pipeline operator (|) to send the value of the here-string to ConvertFrom-StringData. The command saves the result in the $strArray variable, and you can use the same techniques discussed previously to access the strings.

String Operators

As you learned in “Comparison Operators” earlier in the chapter, you can use many operators with strings and arrays. These include –eq, –ne, –lt, –gt, –le, –ge, –like, –notlike, –match, –notmatch, –contains, –notcontains, and –replace.

You also can use the following special operators with strings:

N = Assigns a string value to a variable

N + Concatenates strings by adding them together N * Repeats a string some number of times

DATA DisplayNotes {

ConvertFrom-StringData -stringdata @' Note1 = This appears to be the wrong syntax. Note2 = There is a value missing.

Note3 = Cannot connect at this time. '@

}

$string = @'

Note1 = This appears to be the wrong syntax. Note2 = There is a value missing.

Note3 = Cannot connect at this time. '@

N –Join Joins strings by adding them together with or without delimiters N –Split Splits a string on spaces or a specifi ed delimiter

N –f Formats a string using the extended formatting syntax

The most common string operations you’ll want to perform are assignment and concatenation. As you’ve seen in previous examples, you assign values to strings using the equal sign, such as

$myString = "This is a String."

Concatenation is the technical term for adding strings together. The normal operator for string concatenation is the + operator. Using the + operator, you can add strings together as follows:

$streetAdd = "123 Main St." $cityState = "Anywhere, NY" $zipCode = "12345"

$custAddress = $streetAdd + " " + $cityState + " " + $zipCode $custAddress

123 Main St. Anywhere, NY 12345

Sometimes you might also want to add a string stored in a variable to output you are displaying. You can do this simply by referencing the variable containing the string as part of the double-quoted output string as shown here:

$company = "XYZ Company"

Write-Host "The company is: $company"

The company is: XYZ Company

Concatenation of arrays works much like concatenation of strings. Using the + operator, you can add arrays together as shown in this example:

$array1 = "PC85", "PC25", "PC92"

$array2 = "SERVER41", "SERVER32", "SERVER87" $joinedArray = $array1 + $array2

$joinedArray

PC85 PC25 PC92 SERVER41

$myString = "This is a String."

$streetAdd = "123 Main St." $cityState = "Anywhere, NY" $zipCode = "12345"

$custAddress = $streetAdd + " " + $cityState + " " + $zipCode $custAddress

123 Main St. Anywhere, NY 12345

$company = "XYZ Company"

Write-Host "The company is: $company"

The company is: XYZ Company

$array1 = "PC85", "PC25", "PC92"

$array2 = "SERVER41", "SERVER32", "SERVER87" $joinedArray = $array1 + $array2

$joinedArray

PC85 PC25 PC92 SERVER41

Multiplication of strings and arrays can be handy when you want to repeat the character in a string or the values in an array a specifi ed number of times. For ex- ample, if you have a string with the + character stored in it, you might want to make an 80-character string of + characters act as a dividing line in output. You can do this as shown in the following example and sample output:

$separator = "+"

$sepLine = $separator * 60 $sepLine

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

You use the –Join and –Split operators to join and split strings, respectively. The basic syntaxes for joining strings are

-Join (String1, String2, String3 …)

String1, String2, String3 … -Join "Delimiter"

The fi rst syntax simply joins a collection of strings together. The second syntax joins a collection of strings while specifying a delimiter to use between them. To see how you can join strings without delimiters, consider the following example and sample output:

$a = -join ("abc", "def", "ghi") $a

abcdefghi

To see how you can join strings with delimiters, consider the following example and sample output:

$a = "abc", "def", "ghi" –join ":" $a

abc:def:ghi

The basic syntaxes for splitting strings are

-Split String

String -Split "Delimiter" [,MaxSubStrings]

$separator = "+"

$sepLine = $separator * 60 $sepLine

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

-Join (String1, String2, String3 …)3

String1, String2, String3 … -Join "3 Delimiter"

$a = -join ("abc", "def", "ghi") $a

abcdefghi

$a = "abc", "def", "ghi" –join ":" $a

abc:def:ghi

-Split String

With the fi rst syntax, you can split a string using the spaces between words as delimiters. To see how, consider the following example and sample output:

$a = "abc def ghi" -Split $a

abc def ghi

You also can split strings based on a delimiter as shown in this example:

$a = "jkl:mno:pqr" $a -Split ":"

jkl mno pqr

You use the –f operator to format a string using the extended formatting syntax. With this syntax, you specify exactly how you want to format a series of numeric, alphabetic, or alphanumeric values.

The basic structure is to specify the desired formatting on the left of the –f operator and the values to format in a comma-separated list on the right, as shown in this example:

'FormatingInstructions' –f "Value1", "Value2", "Value3", …

In the formatting instructions, {0} represents the fi rst value, {1} the second value, and so on. Knowing this, you can use the formatting instructions to modify the or- der of values, convert values to different formats, or both. In the following example and sample output, you reverse the order of the values in the output:

'{2} {1} {0}' -f "Monday", "Tuesday", "Wednesday"

Wednesday Tuesday Monday

You are not limited to forward or reverse order. You control the output order and can specify any desired order, such as

'{2} {0} {1}' -f "Cloudy", "Sunny", "Rainy" $a = "abc def ghi"

-Split $a abc def ghi $a = "jkl:mno:pqr" $a -Split ":" jkl mno pqr

'FormatingInstructions' –f "Value1", "Value2", "Value3", …

'{2} {1} {0}' -f "Monday", "Tuesday", "Wednesday"

Wednesday Tuesday Monday

The number of formatting instructions must exactly match the number of values to format. Otherwise, values will be omitted, which is fi ne if this is your intention. In the following example, you omit the second value:

'{0} {2}' -f "Server15", "Server16", "Server17"

Server15 Server17

To each individual formatting instruction, you can add one of the conversion indicators listed in Table 5-18.

TABLE 5-18 Conversion Indicators for Formatting FORMAT

SPECIFIER DESCRIPTION EXAMPLE OUTPUT

:c or :C Converts a numeric format to currency format (based on the computer’s locale).

‘{0:c}’ –f 145.50 $145.50

:e or :E Converts to scientifi c ( exponential) notation. Add a numeric value to specify precision (the number of digits past the decimal point).

‘{0:e4}’ –f [Math]::Pi

3.1416e+000

:f or :F Converts to fi xed-point nota- tion. Add a numeric value to specify precision (the number of digits past the decimal point).

‘{0:f4}’ –f [Math]::Pi

3.1426

:g or :G Converts to the most compact notation, either fi xed-point or scientifi c notation. Add a numeric value to specify the number of signifi cant digits.

‘{0:g3}’ –f [Math]::Pi

3.14

:n or :N Converts to a number with culture-specifi c separators between number groups. Add a numeric value to specify preci- sion (the number of digits past the decimal point).

‘{0:n2}’ –f 1GB 1,073,741,824.00

:p or :P Converts a numeric value to a percentage. Add a numeric value to specify precision (the number of digits past the decimal point).

‘{0:p2}’ –f .112 11.20 %

'{0} {2}' -f "Server15", "Server16", "Server17"

TABLE 5-18 Conversion Indicators for Formatting FORMAT

SPECIFIER DESCRIPTION EXAMPLE OUTPUT

:r or :R Converts a number with precision to guarantee the original value is returned if parsing is reversed. ‘{0:r}’ –f (1GB/2.0) 536870912 Note: (536870912 * 2) = 1,073,741,824 :x or :X Converts the numeric value to

hexadecimal format. ‘{0:x}’ –f 12345678 bc614e {N:hh} : {N:mm} : {N:ss}

Converts a datetime object to a two-digit hour, minute, and second format. You can omit any portion to get a subset.

‘{0:hh:0:mm}’ –f (get-date)

12:35

{N:ddd} Converts a datetime object to a day of the week. It can be combined with the previous format listed.

‘{0:ddd} {0:hh:0:mm}’ –f (get-date)

Mon 12:57

In document Windows PowerShell 2.0 (Page 170-175)