Search code examples
powershellindexof

How to save in array all positions of certain character in a string?


I have a string that contains numerous slashes, for example: $message = ab/cde/f/ghijklm/no/p

I'd like to have a function which when run on this $message will return a $slash_array with the values of the position of all the slashes.

i.e., in my example above, I would have :

PS > Write-Output $slash_array
3,7,9,17,20

Then I want to use these values to insert a slash after every character except if it's at one of these positions (I don't want the program to add a slash after an already existing slash).

============================= EDIT ============================

First, a little side-note : you might find me impolite, but @phcluv removed all the introductory sentences (is that normal in stackoverflow ? pretty new here...)

I tried to simplify the problem here, but in reality :

$message = "my name is silloky"

I want a slash (/) between each character, and a double-slash (//) between 2 words, so my messsage would look like this : m/y//n/a/m/e//i/s//s/i/l/l/o/k/y (or with a slash at the very end, I don't really care).

So I had the idea of replacing the spaces in the original message by 1 slash, and then insert slashes after every character (and so if there's already a slash there, that'll make 2 slashes), except if it were a slash (we would have the slashes of replace, of insert after character before it, and insert after the slash which is a character, which would make 3 slashes). I thought PS wouldn't be able to tell if the current character is a /, so I figured I was better off with an array of the positions insert would ignore. So that's when I called you in...

I have a couple of ideas inspired by all of yours... Is it OK if I post an answer myself and accept it ? Or should I post a definitive answer but accept the most helpful answer ?


Solution

  • Right, so, 3 possibilities here :

    First Possibility

    Very simple (and I found this by mistake, I don't even understand how it can work, but anyway), and in fact there was no need for the arrays of / ! Sorry about that 😭

    $message = "my name is silloky"
    [int] $length = $message.Length
    [int] $times = 0
    [int] $char_num = 1
    do{
        $message = $message.Insert($char_num,'/')
        $char_num = $char_num + 2
        $times = $times + 1
    }until($times -eq $length)
    $message = $message.replace(' ','')
    Write-Output "The slashed output of the message is : $message
    

    Second Possibility

    Using @phuclv's extremely simplistic code that makes me feel like an utter complicator!...

    The ([^/])(?!/) actually replaces my do-until loop from above, but it still leaves some spaces !

    $message = "my name is silloky"
    $message $message -replace '([^/])(?!/)', '$1/'
    $message = $message.replace(' ','')
    Write-Output $message
    

    Third possibility

    Screw this one, it's unnecessarily complicated... 😁

    Conclusion

    BTW, I've tested the code I provid here, but I can't garantee...

    I think I'll take the first possibility because I'm supposed to do this code by myself, I don't really care if my code is 10 lines longer (it makes it look cooler to non-coders...).

    But thanks to @phuclv, @mkelement0, @zett42 (even though you haven't taken part in this conversation, you helped me indirectly) and @Theo anyways for having helped me on this one.

    Whoever you are reading this, feel free to suggest another possibility in the comments if you come across one (or if one of my code snippets diesn't work)! 🧠💡