How to solve SonarQube complaints about my code?

PatPatPat

I'd like to know how to get rid of all non-alphabet in a string please.

I have a String s, where many non-alphabetical characters can be inside. For instance, space, dots, slashes, and any other crazy stuff that are not from a, b, c ... z and not from A, B, C ... Z.

I just want to retain those a, b, c ... z and A, B, C ... Z.

Hence, I wrote:

private static String getGoodString(String s) {
    return s.replaceAll("[^a-zA-Z]", "");
}

This is actually working, quite happy.

However, SonarQube is complaining with:

Refactor this code to use a "static final" Pattern.

Replace these character ranges with Unicode-aware character classes.

How may I achieve the same (get ride of any non-alphabet), while making SonarQube very happy at the same time please?

Tim Biegeleisen

Try using this version:

// inside your class
private static final Pattern p = Pattern.compile("[^\\p{Alpha}]");

private static String getGoodString(String s) {
    return p.matcher(s).replaceAll("");
}

Here I am using a static final Pattern. Also, the regex \p{Alpha} is the Unicode version for matching any letter character.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to solve SonarQube complaints about my code?

From Dev

How to print data about the execution of my code ?

From Dev

How to solve the Total of my html code that linking with input type = "number"

From Dev

My code is leaking. How can I solve it?

From Dev

How to solve the Total of my html code that linking with input type = "number"

From Dev

How to solve my sluggish code (using PHP - PDO )

From Dev

Chef complaints about invalid Unicode escape

From Dev

How do I improve my Python code about heap sort?

From Dev

How inform SonarQube about a package rename?

From Dev

How inform SonarQube about a package rename?

From Dev

How to solve ArrayIndexOutOfBoundsException in this code?

From Dev

How to solve ArrayIndexOutOfBoundsException in this code?

From Dev

How can I solve a SonarQube complainer in for-loop?

From Dev

How to solve StringIndexOutOfBoundsException in my case?

From Dev

How to login into SonarQube in my local box

From Dev

How to solve this situation about ffmpeg with Android NDK

From Dev

How to solve an error about incompatible types?

From Dev

How to solve this error?about node.js

From Dev

Maven complaints about Joda-Time even though I installed it

From Dev

Double colon in class name and RubyMine complaints about short name

From Dev

E/RecyclerView: No adapter attached; skipping layout how can solve it below is my code

From Dev

How can i solve my code to check size of data type (is c language with gcc compiler) on linux mint

From Dev

how can i solve this error in my win32 reading image file code

From Dev

How to get new code coverage in Sonarqube?

From Dev

Need an explanation about my missing code output

From Dev

How can I get informations within code about the cache of my app?

From Dev

How do I go about making this part of my code reactive? (Meteor + React)

From Dev

How to solve my error LNK2019

From Java

How to solve CORS problem of my Django API?

Related Related

  1. 1

    How to solve SonarQube complaints about my code?

  2. 2

    How to print data about the execution of my code ?

  3. 3

    How to solve the Total of my html code that linking with input type = "number"

  4. 4

    My code is leaking. How can I solve it?

  5. 5

    How to solve the Total of my html code that linking with input type = "number"

  6. 6

    How to solve my sluggish code (using PHP - PDO )

  7. 7

    Chef complaints about invalid Unicode escape

  8. 8

    How do I improve my Python code about heap sort?

  9. 9

    How inform SonarQube about a package rename?

  10. 10

    How inform SonarQube about a package rename?

  11. 11

    How to solve ArrayIndexOutOfBoundsException in this code?

  12. 12

    How to solve ArrayIndexOutOfBoundsException in this code?

  13. 13

    How can I solve a SonarQube complainer in for-loop?

  14. 14

    How to solve StringIndexOutOfBoundsException in my case?

  15. 15

    How to login into SonarQube in my local box

  16. 16

    How to solve this situation about ffmpeg with Android NDK

  17. 17

    How to solve an error about incompatible types?

  18. 18

    How to solve this error?about node.js

  19. 19

    Maven complaints about Joda-Time even though I installed it

  20. 20

    Double colon in class name and RubyMine complaints about short name

  21. 21

    E/RecyclerView: No adapter attached; skipping layout how can solve it below is my code

  22. 22

    How can i solve my code to check size of data type (is c language with gcc compiler) on linux mint

  23. 23

    how can i solve this error in my win32 reading image file code

  24. 24

    How to get new code coverage in Sonarqube?

  25. 25

    Need an explanation about my missing code output

  26. 26

    How can I get informations within code about the cache of my app?

  27. 27

    How do I go about making this part of my code reactive? (Meteor + React)

  28. 28

    How to solve my error LNK2019

  29. 29

    How to solve CORS problem of my Django API?

HotTag

Archive