Removing every other character in a string using Java regex

前端 未结 3 1127
一向
一向 2020-12-31 15:29

I have this homework problem where I need to use regex to remove every other character in a string.

In one part, I have to delete characters at index 1,3,5,... I hav

3条回答
  •  一整个雨季
    2020-12-31 16:18

    my regex is only incorrect if the input string length is odd. if it's even, then my regex works fine.

    Change your expresion to .(.)? - the question mark makes the second character optional, which means it doesn't matter if input is odd or even

提交回复
热议问题