How to get the new value of new replaced string? [duplicate]
This question already has an answer here:
String not replacing characters
5 answers
replace String with another in java
5 answers
Im still new at java. Is there a way to get the new string that have been replaced ?
import java.io.*;
public class Test {
public static void main(String args) {
String str = new String("wew");
System.out.println(str.replaceAll("w", "61"));
System.out.println(str.replaceAll("e", "31"));
}
}
output:
61e61
w31w
Desired new output:
613161
I want to get the output string 61e61
then replaced the e
to 31
java string
marked as duplicate by AxelH, Nicholas K, Ravi, Mark Rotteveel
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 31 '18 at 10:03
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
String not replacing characters
5 answers
replace String with another in java
5 answers
Im still new at java. Is there a way to get the new string that have been replaced ?
import java.io.*;
public class Test {
public static void main(String args) {
String str = new String("wew");
System.out.println(str.replaceAll("w", "61"));
System.out.println(str.replaceAll("e", "31"));
}
}
output:
61e61
w31w
Desired new output:
613161
I want to get the output string 61e61
then replaced the e
to 31
java string
marked as duplicate by AxelH, Nicholas K, Ravi, Mark Rotteveel
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 31 '18 at 10:03
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
Java String is immutable.replaceAll()
returns a new String.
– Abhyudaya Sharma
Dec 31 '18 at 6:52
@AxelH not exactly, this question is more about chaining the two replacements, so as to perform both of them.
– nullpointer
Dec 31 '18 at 6:57
2
I agree @nullpointer but the answer show the correct usage of the feature. Sadly, none mentioned the immutability ofString
...
– AxelH
Dec 31 '18 at 7:02
2
@AxelH While I guess it's nice to know that Strings are immutable, I'd rather say it's not the main part to be learnt here. I did rather focus on the fact that the replaceAll method returns a new String (which is due to the immutability, that's correct) which he has to work with. Nice to know the background though.
– maio290
Dec 31 '18 at 7:22
Well @maio290, I feel that without understanding the immutable part ofString
, the result of chaining methods won't be as expected. So, to me, this is the most important part. I tried to quickly explain that in my answer.
– AxelH
Dec 31 '18 at 8:05
add a comment |
This question already has an answer here:
String not replacing characters
5 answers
replace String with another in java
5 answers
Im still new at java. Is there a way to get the new string that have been replaced ?
import java.io.*;
public class Test {
public static void main(String args) {
String str = new String("wew");
System.out.println(str.replaceAll("w", "61"));
System.out.println(str.replaceAll("e", "31"));
}
}
output:
61e61
w31w
Desired new output:
613161
I want to get the output string 61e61
then replaced the e
to 31
java string
This question already has an answer here:
String not replacing characters
5 answers
replace String with another in java
5 answers
Im still new at java. Is there a way to get the new string that have been replaced ?
import java.io.*;
public class Test {
public static void main(String args) {
String str = new String("wew");
System.out.println(str.replaceAll("w", "61"));
System.out.println(str.replaceAll("e", "31"));
}
}
output:
61e61
w31w
Desired new output:
613161
I want to get the output string 61e61
then replaced the e
to 31
This question already has an answer here:
String not replacing characters
5 answers
replace String with another in java
5 answers
java string
java string
edited Dec 31 '18 at 9:25
Rajendra arora
1,50211018
1,50211018
asked Dec 31 '18 at 6:49
Ror SchachRor Schach
504
504
marked as duplicate by AxelH, Nicholas K, Ravi, Mark Rotteveel
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 31 '18 at 10:03
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by AxelH, Nicholas K, Ravi, Mark Rotteveel
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 31 '18 at 10:03
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
Java String is immutable.replaceAll()
returns a new String.
– Abhyudaya Sharma
Dec 31 '18 at 6:52
@AxelH not exactly, this question is more about chaining the two replacements, so as to perform both of them.
– nullpointer
Dec 31 '18 at 6:57
2
I agree @nullpointer but the answer show the correct usage of the feature. Sadly, none mentioned the immutability ofString
...
– AxelH
Dec 31 '18 at 7:02
2
@AxelH While I guess it's nice to know that Strings are immutable, I'd rather say it's not the main part to be learnt here. I did rather focus on the fact that the replaceAll method returns a new String (which is due to the immutability, that's correct) which he has to work with. Nice to know the background though.
– maio290
Dec 31 '18 at 7:22
Well @maio290, I feel that without understanding the immutable part ofString
, the result of chaining methods won't be as expected. So, to me, this is the most important part. I tried to quickly explain that in my answer.
– AxelH
Dec 31 '18 at 8:05
add a comment |
1
Java String is immutable.replaceAll()
returns a new String.
– Abhyudaya Sharma
Dec 31 '18 at 6:52
@AxelH not exactly, this question is more about chaining the two replacements, so as to perform both of them.
– nullpointer
Dec 31 '18 at 6:57
2
I agree @nullpointer but the answer show the correct usage of the feature. Sadly, none mentioned the immutability ofString
...
– AxelH
Dec 31 '18 at 7:02
2
@AxelH While I guess it's nice to know that Strings are immutable, I'd rather say it's not the main part to be learnt here. I did rather focus on the fact that the replaceAll method returns a new String (which is due to the immutability, that's correct) which he has to work with. Nice to know the background though.
– maio290
Dec 31 '18 at 7:22
Well @maio290, I feel that without understanding the immutable part ofString
, the result of chaining methods won't be as expected. So, to me, this is the most important part. I tried to quickly explain that in my answer.
– AxelH
Dec 31 '18 at 8:05
1
1
Java String is immutable.
replaceAll()
returns a new String.– Abhyudaya Sharma
Dec 31 '18 at 6:52
Java String is immutable.
replaceAll()
returns a new String.– Abhyudaya Sharma
Dec 31 '18 at 6:52
@AxelH not exactly, this question is more about chaining the two replacements, so as to perform both of them.
– nullpointer
Dec 31 '18 at 6:57
@AxelH not exactly, this question is more about chaining the two replacements, so as to perform both of them.
– nullpointer
Dec 31 '18 at 6:57
2
2
I agree @nullpointer but the answer show the correct usage of the feature. Sadly, none mentioned the immutability of
String
...– AxelH
Dec 31 '18 at 7:02
I agree @nullpointer but the answer show the correct usage of the feature. Sadly, none mentioned the immutability of
String
...– AxelH
Dec 31 '18 at 7:02
2
2
@AxelH While I guess it's nice to know that Strings are immutable, I'd rather say it's not the main part to be learnt here. I did rather focus on the fact that the replaceAll method returns a new String (which is due to the immutability, that's correct) which he has to work with. Nice to know the background though.
– maio290
Dec 31 '18 at 7:22
@AxelH While I guess it's nice to know that Strings are immutable, I'd rather say it's not the main part to be learnt here. I did rather focus on the fact that the replaceAll method returns a new String (which is due to the immutability, that's correct) which he has to work with. Nice to know the background though.
– maio290
Dec 31 '18 at 7:22
Well @maio290, I feel that without understanding the immutable part of
String
, the result of chaining methods won't be as expected. So, to me, this is the most important part. I tried to quickly explain that in my answer.– AxelH
Dec 31 '18 at 8:05
Well @maio290, I feel that without understanding the immutable part of
String
, the result of chaining methods won't be as expected. So, to me, this is the most important part. I tried to quickly explain that in my answer.– AxelH
Dec 31 '18 at 8:05
add a comment |
5 Answers
5
active
oldest
votes
You can chain replaceAll
as:
System.out.println(str.replaceAll("w", "61").replaceAll("e", "31"));
Currently, you're returning two different strings with both your print statements.
System.out.println(str.replaceAll("w", "61")); // returns new string '61e61'
System.out.println(str.replaceAll("e", "31")); // returns new string 'w31w'
add a comment |
You're using it wrong. The method replaceAll
of the Class String returns a String.
You have to use the return value again (which can be written in one line):
String str = "wew".replaceAll("w", "61").replaceAll("e", "31");
System.out.println(str);
Outputs: 613161
thank you so much sir!. I will try my best learn more on JAVA
– Ror Schach
Dec 31 '18 at 6:58
add a comment |
Let's review why your code doesn't provide what you expect.
You create an instance with "wew".
String str = new String("wew");
Then you replace "w" in "wew" with "61" and print the result "61e61"
System.out.println(str.replaceAll("w", "61")); //61e61
Now, the important part here is that the result of str.replaceAll
is a new instance, so str
is still "wew".
System.out.println(str); // wew
This explain why the second replacement will print "w31w"
System.out.println(str.replaceAll("e", "31")); //w31w
The reason is that String
are immutable, so when you try to change the value of an immutable instance, a new instance is return. So to keep it, you need to assign that instance to a variable.
str = str.replaceAll("w", "61");
Now, the result is kept in the variable "str"
System.out.println(str); // 61e61
Now, one good thing about immutable classes is that method can usually be chained because the return value is an instance of the class itself. So you can call multiple method at one.
str = str.replaceAll("w", "61").replaceAll("e", "31");
System.out.println(str); // 613161
But if you want to print the intermediate result, you will need do it in two statement
str = str.replaceAll("w", "61");
System.out.println(str); // 61e61
System.out.println(str = str.replaceAll("e", "31")); // 613161
Note the last statement, it is possible to merge both assignment and print statement.
First str = ..
will be evaluated then the result will be printed.
add a comment |
String.replaceAll()
will return a new String so that your code System.out.println(str.replaceAll("w", "61"));
and System.out.println(str.replaceAll("e", "31"));
need to be chained, else will return wrong result,
you can use StringUtils.replaceEach() from commons-lang3
:
StringUtils.replaceEach("wew", new String{"w", "e"}, new String{"61", "31"});
Two reasons why I don't like this answer: 1) This does not tell the OP why his solution didn't work. 2) Importing a library shouldn't be always the solution: That's like "hey, my car doesn't drive any more" - "oh, just use this new car here".
– maio290
Dec 31 '18 at 7:20
If your project already used that library and if it offers lot of efficiency and simplicity, I think should just be fine.
– m fauzan abdi
Dec 31 '18 at 8:07
add a comment |
str = str.replaceAll("w", "61") //Output = 61e61
str = str.replaceAll("e", "31") //Output = 613161
2
Your answer contains the solution, but please add a modicum of explanation in addition to your code to show what the problem was and how you solve it.
– ernest_k
Dec 31 '18 at 6:57
thank you so much sir!. i really appreciate it sir
– Ror Schach
Dec 31 '18 at 6:59
add a comment |
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can chain replaceAll
as:
System.out.println(str.replaceAll("w", "61").replaceAll("e", "31"));
Currently, you're returning two different strings with both your print statements.
System.out.println(str.replaceAll("w", "61")); // returns new string '61e61'
System.out.println(str.replaceAll("e", "31")); // returns new string 'w31w'
add a comment |
You can chain replaceAll
as:
System.out.println(str.replaceAll("w", "61").replaceAll("e", "31"));
Currently, you're returning two different strings with both your print statements.
System.out.println(str.replaceAll("w", "61")); // returns new string '61e61'
System.out.println(str.replaceAll("e", "31")); // returns new string 'w31w'
add a comment |
You can chain replaceAll
as:
System.out.println(str.replaceAll("w", "61").replaceAll("e", "31"));
Currently, you're returning two different strings with both your print statements.
System.out.println(str.replaceAll("w", "61")); // returns new string '61e61'
System.out.println(str.replaceAll("e", "31")); // returns new string 'w31w'
You can chain replaceAll
as:
System.out.println(str.replaceAll("w", "61").replaceAll("e", "31"));
Currently, you're returning two different strings with both your print statements.
System.out.println(str.replaceAll("w", "61")); // returns new string '61e61'
System.out.println(str.replaceAll("e", "31")); // returns new string 'w31w'
answered Dec 31 '18 at 6:52
nullpointernullpointer
47.4k11100191
47.4k11100191
add a comment |
add a comment |
You're using it wrong. The method replaceAll
of the Class String returns a String.
You have to use the return value again (which can be written in one line):
String str = "wew".replaceAll("w", "61").replaceAll("e", "31");
System.out.println(str);
Outputs: 613161
thank you so much sir!. I will try my best learn more on JAVA
– Ror Schach
Dec 31 '18 at 6:58
add a comment |
You're using it wrong. The method replaceAll
of the Class String returns a String.
You have to use the return value again (which can be written in one line):
String str = "wew".replaceAll("w", "61").replaceAll("e", "31");
System.out.println(str);
Outputs: 613161
thank you so much sir!. I will try my best learn more on JAVA
– Ror Schach
Dec 31 '18 at 6:58
add a comment |
You're using it wrong. The method replaceAll
of the Class String returns a String.
You have to use the return value again (which can be written in one line):
String str = "wew".replaceAll("w", "61").replaceAll("e", "31");
System.out.println(str);
Outputs: 613161
You're using it wrong. The method replaceAll
of the Class String returns a String.
You have to use the return value again (which can be written in one line):
String str = "wew".replaceAll("w", "61").replaceAll("e", "31");
System.out.println(str);
Outputs: 613161
answered Dec 31 '18 at 6:54
maio290maio290
2,014414
2,014414
thank you so much sir!. I will try my best learn more on JAVA
– Ror Schach
Dec 31 '18 at 6:58
add a comment |
thank you so much sir!. I will try my best learn more on JAVA
– Ror Schach
Dec 31 '18 at 6:58
thank you so much sir!. I will try my best learn more on JAVA
– Ror Schach
Dec 31 '18 at 6:58
thank you so much sir!. I will try my best learn more on JAVA
– Ror Schach
Dec 31 '18 at 6:58
add a comment |
Let's review why your code doesn't provide what you expect.
You create an instance with "wew".
String str = new String("wew");
Then you replace "w" in "wew" with "61" and print the result "61e61"
System.out.println(str.replaceAll("w", "61")); //61e61
Now, the important part here is that the result of str.replaceAll
is a new instance, so str
is still "wew".
System.out.println(str); // wew
This explain why the second replacement will print "w31w"
System.out.println(str.replaceAll("e", "31")); //w31w
The reason is that String
are immutable, so when you try to change the value of an immutable instance, a new instance is return. So to keep it, you need to assign that instance to a variable.
str = str.replaceAll("w", "61");
Now, the result is kept in the variable "str"
System.out.println(str); // 61e61
Now, one good thing about immutable classes is that method can usually be chained because the return value is an instance of the class itself. So you can call multiple method at one.
str = str.replaceAll("w", "61").replaceAll("e", "31");
System.out.println(str); // 613161
But if you want to print the intermediate result, you will need do it in two statement
str = str.replaceAll("w", "61");
System.out.println(str); // 61e61
System.out.println(str = str.replaceAll("e", "31")); // 613161
Note the last statement, it is possible to merge both assignment and print statement.
First str = ..
will be evaluated then the result will be printed.
add a comment |
Let's review why your code doesn't provide what you expect.
You create an instance with "wew".
String str = new String("wew");
Then you replace "w" in "wew" with "61" and print the result "61e61"
System.out.println(str.replaceAll("w", "61")); //61e61
Now, the important part here is that the result of str.replaceAll
is a new instance, so str
is still "wew".
System.out.println(str); // wew
This explain why the second replacement will print "w31w"
System.out.println(str.replaceAll("e", "31")); //w31w
The reason is that String
are immutable, so when you try to change the value of an immutable instance, a new instance is return. So to keep it, you need to assign that instance to a variable.
str = str.replaceAll("w", "61");
Now, the result is kept in the variable "str"
System.out.println(str); // 61e61
Now, one good thing about immutable classes is that method can usually be chained because the return value is an instance of the class itself. So you can call multiple method at one.
str = str.replaceAll("w", "61").replaceAll("e", "31");
System.out.println(str); // 613161
But if you want to print the intermediate result, you will need do it in two statement
str = str.replaceAll("w", "61");
System.out.println(str); // 61e61
System.out.println(str = str.replaceAll("e", "31")); // 613161
Note the last statement, it is possible to merge both assignment and print statement.
First str = ..
will be evaluated then the result will be printed.
add a comment |
Let's review why your code doesn't provide what you expect.
You create an instance with "wew".
String str = new String("wew");
Then you replace "w" in "wew" with "61" and print the result "61e61"
System.out.println(str.replaceAll("w", "61")); //61e61
Now, the important part here is that the result of str.replaceAll
is a new instance, so str
is still "wew".
System.out.println(str); // wew
This explain why the second replacement will print "w31w"
System.out.println(str.replaceAll("e", "31")); //w31w
The reason is that String
are immutable, so when you try to change the value of an immutable instance, a new instance is return. So to keep it, you need to assign that instance to a variable.
str = str.replaceAll("w", "61");
Now, the result is kept in the variable "str"
System.out.println(str); // 61e61
Now, one good thing about immutable classes is that method can usually be chained because the return value is an instance of the class itself. So you can call multiple method at one.
str = str.replaceAll("w", "61").replaceAll("e", "31");
System.out.println(str); // 613161
But if you want to print the intermediate result, you will need do it in two statement
str = str.replaceAll("w", "61");
System.out.println(str); // 61e61
System.out.println(str = str.replaceAll("e", "31")); // 613161
Note the last statement, it is possible to merge both assignment and print statement.
First str = ..
will be evaluated then the result will be printed.
Let's review why your code doesn't provide what you expect.
You create an instance with "wew".
String str = new String("wew");
Then you replace "w" in "wew" with "61" and print the result "61e61"
System.out.println(str.replaceAll("w", "61")); //61e61
Now, the important part here is that the result of str.replaceAll
is a new instance, so str
is still "wew".
System.out.println(str); // wew
This explain why the second replacement will print "w31w"
System.out.println(str.replaceAll("e", "31")); //w31w
The reason is that String
are immutable, so when you try to change the value of an immutable instance, a new instance is return. So to keep it, you need to assign that instance to a variable.
str = str.replaceAll("w", "61");
Now, the result is kept in the variable "str"
System.out.println(str); // 61e61
Now, one good thing about immutable classes is that method can usually be chained because the return value is an instance of the class itself. So you can call multiple method at one.
str = str.replaceAll("w", "61").replaceAll("e", "31");
System.out.println(str); // 613161
But if you want to print the intermediate result, you will need do it in two statement
str = str.replaceAll("w", "61");
System.out.println(str); // 61e61
System.out.println(str = str.replaceAll("e", "31")); // 613161
Note the last statement, it is possible to merge both assignment and print statement.
First str = ..
will be evaluated then the result will be printed.
edited Dec 31 '18 at 8:10
answered Dec 31 '18 at 7:57
AxelHAxelH
10.8k21440
10.8k21440
add a comment |
add a comment |
String.replaceAll()
will return a new String so that your code System.out.println(str.replaceAll("w", "61"));
and System.out.println(str.replaceAll("e", "31"));
need to be chained, else will return wrong result,
you can use StringUtils.replaceEach() from commons-lang3
:
StringUtils.replaceEach("wew", new String{"w", "e"}, new String{"61", "31"});
Two reasons why I don't like this answer: 1) This does not tell the OP why his solution didn't work. 2) Importing a library shouldn't be always the solution: That's like "hey, my car doesn't drive any more" - "oh, just use this new car here".
– maio290
Dec 31 '18 at 7:20
If your project already used that library and if it offers lot of efficiency and simplicity, I think should just be fine.
– m fauzan abdi
Dec 31 '18 at 8:07
add a comment |
String.replaceAll()
will return a new String so that your code System.out.println(str.replaceAll("w", "61"));
and System.out.println(str.replaceAll("e", "31"));
need to be chained, else will return wrong result,
you can use StringUtils.replaceEach() from commons-lang3
:
StringUtils.replaceEach("wew", new String{"w", "e"}, new String{"61", "31"});
Two reasons why I don't like this answer: 1) This does not tell the OP why his solution didn't work. 2) Importing a library shouldn't be always the solution: That's like "hey, my car doesn't drive any more" - "oh, just use this new car here".
– maio290
Dec 31 '18 at 7:20
If your project already used that library and if it offers lot of efficiency and simplicity, I think should just be fine.
– m fauzan abdi
Dec 31 '18 at 8:07
add a comment |
String.replaceAll()
will return a new String so that your code System.out.println(str.replaceAll("w", "61"));
and System.out.println(str.replaceAll("e", "31"));
need to be chained, else will return wrong result,
you can use StringUtils.replaceEach() from commons-lang3
:
StringUtils.replaceEach("wew", new String{"w", "e"}, new String{"61", "31"});
String.replaceAll()
will return a new String so that your code System.out.println(str.replaceAll("w", "61"));
and System.out.println(str.replaceAll("e", "31"));
need to be chained, else will return wrong result,
you can use StringUtils.replaceEach() from commons-lang3
:
StringUtils.replaceEach("wew", new String{"w", "e"}, new String{"61", "31"});
edited Dec 31 '18 at 7:51
answered Dec 31 '18 at 7:08
m fauzan abdim fauzan abdi
34129
34129
Two reasons why I don't like this answer: 1) This does not tell the OP why his solution didn't work. 2) Importing a library shouldn't be always the solution: That's like "hey, my car doesn't drive any more" - "oh, just use this new car here".
– maio290
Dec 31 '18 at 7:20
If your project already used that library and if it offers lot of efficiency and simplicity, I think should just be fine.
– m fauzan abdi
Dec 31 '18 at 8:07
add a comment |
Two reasons why I don't like this answer: 1) This does not tell the OP why his solution didn't work. 2) Importing a library shouldn't be always the solution: That's like "hey, my car doesn't drive any more" - "oh, just use this new car here".
– maio290
Dec 31 '18 at 7:20
If your project already used that library and if it offers lot of efficiency and simplicity, I think should just be fine.
– m fauzan abdi
Dec 31 '18 at 8:07
Two reasons why I don't like this answer: 1) This does not tell the OP why his solution didn't work. 2) Importing a library shouldn't be always the solution: That's like "hey, my car doesn't drive any more" - "oh, just use this new car here".
– maio290
Dec 31 '18 at 7:20
Two reasons why I don't like this answer: 1) This does not tell the OP why his solution didn't work. 2) Importing a library shouldn't be always the solution: That's like "hey, my car doesn't drive any more" - "oh, just use this new car here".
– maio290
Dec 31 '18 at 7:20
If your project already used that library and if it offers lot of efficiency and simplicity, I think should just be fine.
– m fauzan abdi
Dec 31 '18 at 8:07
If your project already used that library and if it offers lot of efficiency and simplicity, I think should just be fine.
– m fauzan abdi
Dec 31 '18 at 8:07
add a comment |
str = str.replaceAll("w", "61") //Output = 61e61
str = str.replaceAll("e", "31") //Output = 613161
2
Your answer contains the solution, but please add a modicum of explanation in addition to your code to show what the problem was and how you solve it.
– ernest_k
Dec 31 '18 at 6:57
thank you so much sir!. i really appreciate it sir
– Ror Schach
Dec 31 '18 at 6:59
add a comment |
str = str.replaceAll("w", "61") //Output = 61e61
str = str.replaceAll("e", "31") //Output = 613161
2
Your answer contains the solution, but please add a modicum of explanation in addition to your code to show what the problem was and how you solve it.
– ernest_k
Dec 31 '18 at 6:57
thank you so much sir!. i really appreciate it sir
– Ror Schach
Dec 31 '18 at 6:59
add a comment |
str = str.replaceAll("w", "61") //Output = 61e61
str = str.replaceAll("e", "31") //Output = 613161
str = str.replaceAll("w", "61") //Output = 61e61
str = str.replaceAll("e", "31") //Output = 613161
answered Dec 31 '18 at 6:53
Cyrus LeungCyrus Leung
995
995
2
Your answer contains the solution, but please add a modicum of explanation in addition to your code to show what the problem was and how you solve it.
– ernest_k
Dec 31 '18 at 6:57
thank you so much sir!. i really appreciate it sir
– Ror Schach
Dec 31 '18 at 6:59
add a comment |
2
Your answer contains the solution, but please add a modicum of explanation in addition to your code to show what the problem was and how you solve it.
– ernest_k
Dec 31 '18 at 6:57
thank you so much sir!. i really appreciate it sir
– Ror Schach
Dec 31 '18 at 6:59
2
2
Your answer contains the solution, but please add a modicum of explanation in addition to your code to show what the problem was and how you solve it.
– ernest_k
Dec 31 '18 at 6:57
Your answer contains the solution, but please add a modicum of explanation in addition to your code to show what the problem was and how you solve it.
– ernest_k
Dec 31 '18 at 6:57
thank you so much sir!. i really appreciate it sir
– Ror Schach
Dec 31 '18 at 6:59
thank you so much sir!. i really appreciate it sir
– Ror Schach
Dec 31 '18 at 6:59
add a comment |
1
Java String is immutable.
replaceAll()
returns a new String.– Abhyudaya Sharma
Dec 31 '18 at 6:52
@AxelH not exactly, this question is more about chaining the two replacements, so as to perform both of them.
– nullpointer
Dec 31 '18 at 6:57
2
I agree @nullpointer but the answer show the correct usage of the feature. Sadly, none mentioned the immutability of
String
...– AxelH
Dec 31 '18 at 7:02
2
@AxelH While I guess it's nice to know that Strings are immutable, I'd rather say it's not the main part to be learnt here. I did rather focus on the fact that the replaceAll method returns a new String (which is due to the immutability, that's correct) which he has to work with. Nice to know the background though.
– maio290
Dec 31 '18 at 7:22
Well @maio290, I feel that without understanding the immutable part of
String
, the result of chaining methods won't be as expected. So, to me, this is the most important part. I tried to quickly explain that in my answer.– AxelH
Dec 31 '18 at 8:05