Splitting a string into separate variables

后端 未结 5 1786
死守一世寂寞
死守一世寂寞 2020-12-13 16:42

I have a string, which I have split using the code $CreateDT.Split(\" \"). I now want to manipulate two separate strings in different ways. How can I separate t

相关标签:
5条回答
  • 2020-12-13 17:18

    An array is created with the -split operator. Like so,

    $myString="Four score and seven years ago"
    $arr = $myString -split ' '
    $arr # Print output
    Four
    score
    and
    seven
    years
    ago
    

    When you need a certain item, use array index to reach it. Mind that index starts from zero. Like so,

    $arr[2] # 3rd element
    and
    $arr[4] # 5th element
    years
    
    0 讨论(0)
  • 2020-12-13 17:21

    It is important to note the following difference between the two techniques:

    $Str="This is the<BR />source string<BR />ALL RIGHT"
    $Str.Split("<BR />")
    This
    is
    the
    (multiple blank lines)
    source
    string
    (multiple blank lines)
    ALL
    IGHT
    $Str -Split("<BR />")
    This is the
    source string
    ALL RIGHT 
    

    From this you can see that the string.split() method:

    • performs a case sensitive split (note that "ALL RIGHT" his split on the "R" but "broken" is not split on the "r")
    • treats the string as a list of possible characters to split on

    While the -split operator:

    • performs a case-insensitive comparison
    • only splits on the whole string
    0 讨论(0)
  • 2020-12-13 17:22

    Try this:

    $Object = 'FirstPart SecondPart' | ConvertFrom-String -PropertyNames Val1, Val2
    $Object.Val1
    $Object.Val2
    
    0 讨论(0)
  • 2020-12-13 17:28

    Foreach-object operation statement:

    $a,$b = 'hi.there' | foreach split .
    $a,$b
    
    hi
    there
    
    0 讨论(0)
  • 2020-12-13 17:29

    Like this?

    $string = 'FirstPart SecondPart'
    $a,$b = $string.split(' ')
    $a
    $b
    
    0 讨论(0)
提交回复
热议问题