Testing for repeated characters in a string

后端 未结 11 806
闹比i
闹比i 2020-12-16 22:55

I\'m doing some work with strings, and I have a scenario where I need to determine if a string (usually a small one < 10 characters) contains repeated characters.

11条回答
  •  一向
    一向 (楼主)
    2020-12-16 23:34

    /(.).*\1/
    

    (or whatever the equivalent is in your regex library's syntax)

    Not the most efficient, since it will probably backtrack to every character in the string and then scan forward again. And I don't usually advocate regular expressions. But if you want brevity...

提交回复
热议问题