Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Sonar) Fix "String#replace should be preferred to String#replaceAll" #1056

Conversation

pixeebot[bot]
Copy link
Contributor

@pixeebot pixeebot bot commented Apr 9, 2024

Remediation

This change fixes the following Sonar rule: "String#replace should be preferred to String#replaceAll" (id = java:S5361)

Details

This change replaces String#replaceAll() with String#replace() to enhance performance and avoid confusion.

The String#replaceAll() call takes a regular expression for the first argument, which is then compiled and used to replace string subsections. However, the argument being passed to it doesn't actually appear to be a regular expression. Therefore, the replace() API appears to be a better fit.

Our changes look something like this:

    String init = "my string\n";

-   String changed = init.replaceAll("\n", "<br>");
+   String changed = init.replace("\n", "<br>");
More reading

Powered by: pixeebot (codemod ID: sonar:java/substitute-replaceAll-s5361)

Copy link

sonarcloud bot commented Apr 9, 2024

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@Frooodle Frooodle merged commit 5564f37 into main Apr 9, 2024
4 checks passed
@pixeebot pixeebot bot deleted the pixeebot/drip-2024-04-09-sonar-java/substitute-replaceAll-s5361 branch April 9, 2024 05:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant