Find the index of the first digit in a string

前端 未结 14 830
清酒与你
清酒与你 2020-12-28 12:29

I have a string like

\"xdtwkeltjwlkejt7wthwk89lk\"

how can I get the index of the first digit in the string?

14条回答
  •  一整个雨季
    2020-12-28 12:59

    Thought I'd toss my method on the pile. I'll do just about anything to avoid regex.

    sequence = 'xdtwkeltjwlkejt7wthwk89lk'
    i = [x.isdigit() for x in sequence].index(True)
    

    To explain what's going on here:

    • [x.isdigit() for x in sequence] is going to translate the string into an array of booleans representing whether each character is a digit or not
    • [...].index(True) returns the first index value that True is found in.

提交回复
热议问题