How to remove all type of prefix salutation from string in java?

后端 未结 1 1024
日久生厌
日久生厌 2021-01-25 22:43

Suppose I have string coming from Input with name (for eg: Mr . Aditya Jha). How do I remove salutation from the start of input?

List of salutations that ca

相关标签:
1条回答
  • 2021-01-25 23:07

    You may use

    name = name.replaceAll("\\s{2,}", " ").replaceFirst("(?i)^\\s*(?:M(?:iss|rs?|s)|Dr|Rev)\\b[\\s.]*", "").trim();
    

    See the regex demo

    Pattern details

    • (?i) - case ignoring option
    • ^ - start of string
    • \s* - 0+ whitespaces
    • (?:M(?:iss|rs?|s)|Dr|Rev) - M followed with iss, r, rs, s, or Dr or Rev (you may add more after | here)
    • \b - word boundary
    • [\s.]* - 0 or more whitespaces or dots.
    0 讨论(0)
提交回复
热议问题