splitting on multple characters.

Tagged: 

This topic contains 4 replies, has 3 voices, and was last updated by  John Curtiss 1 week, 5 days ago.

  • Author
    Posts
  • #79463

    John Curtiss
    Participant

    $string = "first.last.priv"
    $trimmedstring = $string.split(@('.priv'),'none')

    result appears to be “first.last”, but if I out-gridview and paste it into notepad, it's really “first.last “ (note the gap after 'last'). I assume that's a tab at the end. I get the same result if I don't use the entire “.priv” as the splitter. this of course breaks me feeding $trimmedstring to get-aduser. i get a similar result with

    $trimmedstring = $string -split '.priv'

    am I doing something wrong?

  • #79466

    John Curtiss
    Participant

    here's another

    $string = "student\john.curtiss"
    $string.split(@('student\'),'1') + ".test"

    I expect to get john.curtiss.test back. but instead I get

    john.curtiss
    .test

    same thing if do

    ($string -split 'student\\') + ".test"

  • #79472

    Curtis Smith
    Participant

    Split results in an array, not a string. In this case it results in an array with a single element. When that array is converted to a string, the element is printed with a space after it as an indicator of the separation between the elements. If you want just the value of the first element, you should specifically select it.

    cls
    $string = "first.last.priv"
    $trimmedstring = $string.split(@('.priv'),'none')
    "Outputting the Array as a string: -$trimmedstring-"
    "Outputting just element 0 of the Array: -$($trimmedstring[0])-"

    Results:

    Outputting the Array as a string: -first.last -
    Outputting just element 0 of the Array: -first.last-

    As a better example of it putting a space as a separator of the elements.

    cls
    $string = "first.last.privfirst.last.privfirst.last.priv"
    $trimmedstring = $string.split(@('.priv'),'none')
    "Outputting the Array as a string: -$trimmedstring-"
    "Outputting just element 0 of the Array: -$($trimmedstring[0])-"

    Results:

    Outputting the Array as a string: -first.last first.last first.last -
    Outputting just element 0 of the Array: -first.last-

    Notice that since the string ends with .priv, split sees this as a delimeter and creates a new element in the array for the rest of the string, which in this case is empty. If there were additional data after .priv, then the element would contain that data an not look like a random space.

    cls
    $string = "first.last.privend"
    $trimmedstring = $string.split(@('.priv'),'none')
    "Outputting the Array as a string: -$trimmedstring-"
    "Outputting just element 0 of the Array: -$($trimmedstring[0])-"

    Results:

    Outputting the Array as a string: -first.last end-
    Outputting just element 0 of the Array: -first.last-
  • #79510

    Sam Boutros
    Participant
    $string = "student\john.curtiss"
    [String]$string.split(@('student\'),1) + ".test" # In this example just type cast it as a string
    
    $string = "first.last.priv"
    $string.split(@('.priv'),0) | % { "($_)" } # shows 2nd empty line
    $string.split(@('.priv'),0) | select -First 1 # This is what you want 
    
  • #79535

    John Curtiss
    Participant

    ok. that makes sense. i knew that if I was splitting on just the backslash, I would have to specify the element number, because there would be more than one element. but I guess I thought "splitting" the .priv off the end would just give me a string. i probably also could have used replace or something like that to give me a string.

    thank you both.

You must be logged in to reply to this topic.