Testing whether string starts with or end with another string

后端 未结 2 1676
旧时难觅i
旧时难觅i 2021-02-01 11:40

How should I check whether a string starts or ends with a given string? There doesn\'t seem to be any built-in methods available (or maybe it\'s just the IDE I\'m using that isn

2条回答
  •  囚心锁ツ
    2021-02-01 12:15

    string.start_with?("substring")
    string.end_with?("substring")
    

    Returns true or false

    Source:

    http://ruby-doc.org//core-2.2.0/String.html#method-i-start_with-3F

    http://ruby-doc.org//core-2.2.0/String.html#method-i-end_with-3F

提交回复
热议问题