Can't delete words from a string with replaceAll - java

if(containsAllWeather || containsAllWeather2){
String weatherLocation = value.toString();
if (weatherLocation != null){
weatherLocation.replaceAll("how","")
.replaceAll("what","")
.replaceAll("weather", "")
.replaceAll("like", "")
.replaceAll(" in", "")
.replaceAll(" at", "")
.replaceAll("around", "");
}
weatherLocation still gives exactly what the variable value includes and doesn't delete any of the words listed above.
This worked when I split weatherLocation to an array of strings, say, array of weatherLoc, and those lines of code worked for weatherLoc[1]
what am I doing wrong?

You need to assign the values returned by the method calls back to the String reference variable. Each time you do replaceAll() , it returns a new String object but your weatherLocation variable is still referencing to the original String.
weatherLocation = weatherLocation.replaceAll("how","")
.replaceAll("what","")
.replaceAll("weather", "")
.replaceAll("like", "")
.replaceAll(" in", "")
.replaceAll(" at", "")
.replaceAll("around", "");

Strings are immutable. You need to assign the value of all those replaceAll calls to a variable and that will be what you want.
weatherLocation = weatherLocation.replaceAll("how","")
.replaceAll("what","")
.replaceAll("weather", "")
.replaceAll("like", "")
.replaceAll(" in", "")
.replaceAll(" at", "")
.replaceAll("around", "");

Try this:
weatherLocation = weatherLocation.replaceAll("how","")
.replaceAll("what","")
.replaceAll("weather", "")
.replaceAll("like", "")
.replaceAll(" in", "")
.replaceAll(" at", "")
.replaceAll("around", "");

String is immutable. So String.replaceAll returns a new instance of String. So you need to use like following
weatherLocation = weatherLocation.replaceAll("how","")
.replaceAll("what","")
.replaceAll("weather", "")
.replaceAll("like", "")
.replaceAll(" in", "")
.replaceAll(" at", "")
.replaceAll("around", "");

Related

"How to replace "[" from string in java?"

I want to use the String::replaceall method in Java. I have a string which includes "[" and I want to replace that with "" but it's showing an error.
String str="already data exists = [ abc,xyz,123 ]";
String replacedStr = str.replaceAll("Already Po Exits =", "");
String replacedStr1 = replacedStr.replaceAll("\\[", "");
The following replace function will replace [ in your string.
str.replaceAll("\\[", "")
or you can use replace function to achieve the same
str.replace("[", "")
For replacing Unclosed character, you need to add escape character while replacing
String replacedStr1 = replacedStr.replaceAll("\\[", "");
You can use the replace function to do this.
String replacedStr1 = replacedStr.replace("[", "");

Regex string replace considerably fast than regular replace in kotlin

I have been trying to replace characters in string like below
data = data.replace(Regex("[a-z:]", RegexOption.IGNORE_CASE), "")
.replace(Regex("/", RegexOption.IGNORE_CASE), ".")
.replace(Regex(",", RegexOption.IGNORE_CASE), "")
.replace(Regex("'", RegexOption.IGNORE_CASE), "")
.replace(Regex("é",RegexOption.IGNORE_CASE),"")
.replace(Regex("ê",RegexOption.IGNORE_CASE),"")
.replace(Regex("ö",RegexOption.IGNORE_CASE),"")
.replace(Regex("Ä",RegexOption.IGNORE_CASE),"")
.replace(Regex("ä",RegexOption.IGNORE_CASE),"")
.replace(Regex("ä |",RegexOption.IGNORE_CASE),"")
And
data = data.replace(Regex("[a-z:]", RegexOption.IGNORE_CASE), "")
.replace("/", ".")
.replace(",", "")
.replace("'", "")
.replace("é","")
.replace("ê","")
.replace("ö","")
.replace("Ä","")
.replace("ä","")
And I measured time required for both of this code and surprisingly code with regex turned out at least 20 times faster than normal replace.
As long as I have been reading about regex, they say regex is expensive operation, am I missing something?

Java-8 - replacing "?" character from the start of a Optional.of(String)

I'm trying to replace "?" character or delete it if my string start with ?. I tested this code and it doesn't work:
Optional<String> stringvalue = Optional.of("?test1=search&test2=ok&test3=hello");
String parameterName = "test1";
if( stringvalue.get().startsWith("?") ){
stringvalue.get().replaceFirst("\\?", "");
}
System.out.println(stringvalue.get());
You can use Optional.map as well:
stringvalue = stringvalue.map( (s) -> s.startsWith("?") ? s.replaceFirst("\\?", "") : s );
The answer is in the comment of #Aominè but how? I think you have to use :
if (stringvalue.get().startsWith("?")) {
stringvalue = Optional.of(stringvalue.get().replaceFirst("\\?", ""));
// ^^^^^^^^^^^-------note this
}
Or just :
stringvalue = Optional.of(stringvalue.get().replaceFirst("^\\?", ""));
//------------^^^^^^^^^^^---------------------------------^
Strings are immutable in Java. So stringvalue.get().replaceFirst("\\?", ""); leaves the original String value unchanged. You need to store the result of the replacement like this:
String parameterName = stringvalue.get().replaceFirst("\\?", "");
Also note that you can use ^ avoiding to test if the string startsWith ?:
String parameterName = stringvalue.get().replaceFirst("^\\?", "");

How to use MapBuilder in kotlin and add all values?

I am trying to convert/add below java code in bamboo spec in kotlin but geeting error.Can someone let me know how to use it in KOTLIN?How to use mapbuilder in kotlin which they are using in java.
new AnyTask(new AtlassianModule("ch.mibex.bamboo.sonar4bamboo:sonar4bamboo.gradletask"))
.description("Sonar Gradle")
.configuration(new MapBuilder()
.put("chosenSonarConfigId", "1")
.put("useGradleWrapper", "true")
.put("useNewGradleSonarQubePlugin", "true")
.put("sonarJavaSource", "")
.put("sonarProjectName", "")
.put("buildJdk", "JDK")
.put("gradleWrapperLocation", "${bamboo.build.working.directory}\\XYZ\\gradlew.bat")
.put("sonarLanguage", "")
.put("sonarSources", "")
.put("useGlobalSonarServerConfig", "true")
.put("failBuildForBrokenQualityGates", "")
.put("sonarTests", "")
.put("failBuildForSonarErrors", "")
.put("sonarProjectVersion", "")
.put("sonarBranch", "")
.put("executable", "")
.put("illegalBranchCharsReplacement", "_")
.put("sonarJavaTarget", "")
.put("environmentVariables", "")
.put("replaceSpecialBranchChars", "")
.put("additionalProperties", "")
.put("autoBranch", "")
.put("sonarProjectKey", "")
.put("overrideSonarBuildConfig", "")
.put("workingSubDirectory", "XYZ")
.build()))
I think I need to use MapBuilder in kotlin but I don't know how to use it and all values
In kotlin
AnyTask(AtlassianModule("ch.mibex.bamboo.sonar4bamboo:sonar4bamboo.gradletask"))
.description("Sonar Gradle").configuration(MapBuilder()
.put("chosenSonarConfigId", "1")
.put("useGradleWrapper", "true")
.put("useNewGradleSonarQubePlugin", "true")
.put("sonarJavaSource", "")
.put("sonarProjectName", "")
.put("buildJdk", "JDK")
.put("gradleWrapperLocation", "\${bamboo.build.working.directory}/gradlew.bat")
.put("sonarLanguage", "")
.put("sonarSources", "")
.put("useGlobalSonarServerConfig", "true")
.put("failBuildForBrokenQualityGates", "")
.put("sonarTests", "")
.put("failBuildForSonarErrors", "")
.put("sonarProjectVersion", "")
.put("sonarBranch", "")
.put("executable", "")
.put("illegalBranchCharsReplacement", "_")
.put("sonarJavaTarget", "")
.put("environmentVariables", "")
.put("replaceSpecialBranchChars", "")
.put("additionalProperties", "")
.put("autoBranch", "")
.put("sonarProjectKey", "")
.put("overrideSonarBuildConfig", "")
.put("workingSubDirectory", "")
.build())
To solve that, you should specify the types, as suggested:
configuration(MapBuilder<String,String>().put...
But, there's a more straightforward way in Kotlin using its mapOf:
mapOf("chosenSonarConfigId" to "1",
"useNewGradleSonarQubePlugin" to "true",
"sonarJavaSource" to "true"
//...
)

String.replaceAll(string,string) method gives unexpexted output

I have some amounts as input like:
Rs1 ,
INR10,954.00 ,
INR 45000 ,
INR 25000.70 ,
Rs.25000 ,
Rs.1,000 ,
Rs. 14000
these are the input formats I'm using
String getRuppee="Rs1"; // this string can not get the format and gives wrong output.
String val=getRuppee.toLowerCase()
.replaceAll(",", "")
.replaceAll("rs.", "")
.replaceAll("\"rs\"", "")
.replaceAll("rs", "")
.replaceAll("inr", "")
.replaceAll("inr ", "")
.replaceAll("mrp", "")
.replaceAll(" ", "");
This is how i get the output as per input show above, unless the first (Rs1).
Log.e("Converstion", "Converstion Balance."
+getRuppee.toLowerCase().
replaceAll("rs.", ""));
i need the output 1 but it gives me null.
Logcat Displays: "06-07 11:34:48.438: E/Converstion(8233): Converstion Balance."
Change your code as follows
String getRuppee="Rs1"; // this string can not get the format and gives wrong output.
String val=getRuppee.toLowerCase()
.replace(",", "")
.replace("rs.", "")
.replace("\"rs\"", "")
.replace("rs", "")
.replace("inr", "")
.replace("inr ", "")
.replace("mrp", "")
.replace(" ", "");
This is your problem here
.replaceAll("rs.", "")
As replaceAll is doing replacement based upon regexp, then this is replacing anything starting with rs followed by any char
Try using .replace instead which replaces all strings
Try below code:
String[] getRuppee = {"RS1", "INR10","954.00" , "INR 45000" , "INR 25000.70" , "Rs.25000" , "Rs.1,000" , "Rs. 14000"};
for (String tmp : getRuppee){
String val=tmp.toLowerCase()
.replaceAll(",", "")
.replaceAll("rs\\.", "") //check here
.replaceAll("\"rs\"", "")
.replaceAll("rs", "")
.replaceAll("inr", "")
.replaceAll("inr ", "")
.replaceAll("mrp", "")
.replaceAll(" ", "");
System.out.println(val);
}

Categories