What is equivalent to .replaceSecond or .replaceThird?
up vote
3
down vote
favorite
In this code, we remove the substring "luna" from email
string using the .replaceFirst
method. We are removing the characters in between + and @. But this happens only in the first instance because we used .replaceFirst
. What if we wanted to target the second instance of + and @ to remove "smith"?
Our output now is alice+@john+smith@steve+oliver@
but we want alice+luna@john+@steve+oliver@
public class Main {
public static void main(String args) {
String email = "alice+luna@john+smith@steve+oliver@";
String newEmail = email.replaceFirst("\+.*?@", "");
System.out.println(newEmail);
}
}
java string replace
add a comment |
up vote
3
down vote
favorite
In this code, we remove the substring "luna" from email
string using the .replaceFirst
method. We are removing the characters in between + and @. But this happens only in the first instance because we used .replaceFirst
. What if we wanted to target the second instance of + and @ to remove "smith"?
Our output now is alice+@john+smith@steve+oliver@
but we want alice+luna@john+@steve+oliver@
public class Main {
public static void main(String args) {
String email = "alice+luna@john+smith@steve+oliver@";
String newEmail = email.replaceFirst("\+.*?@", "");
System.out.println(newEmail);
}
}
java string replace
1
Use a regex to do this?
– triplem
Nov 19 at 10:11
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
In this code, we remove the substring "luna" from email
string using the .replaceFirst
method. We are removing the characters in between + and @. But this happens only in the first instance because we used .replaceFirst
. What if we wanted to target the second instance of + and @ to remove "smith"?
Our output now is alice+@john+smith@steve+oliver@
but we want alice+luna@john+@steve+oliver@
public class Main {
public static void main(String args) {
String email = "alice+luna@john+smith@steve+oliver@";
String newEmail = email.replaceFirst("\+.*?@", "");
System.out.println(newEmail);
}
}
java string replace
In this code, we remove the substring "luna" from email
string using the .replaceFirst
method. We are removing the characters in between + and @. But this happens only in the first instance because we used .replaceFirst
. What if we wanted to target the second instance of + and @ to remove "smith"?
Our output now is alice+@john+smith@steve+oliver@
but we want alice+luna@john+@steve+oliver@
public class Main {
public static void main(String args) {
String email = "alice+luna@john+smith@steve+oliver@";
String newEmail = email.replaceFirst("\+.*?@", "");
System.out.println(newEmail);
}
}
java string replace
java string replace
edited Nov 19 at 10:09
Prashant Gupta
741519
741519
asked Nov 19 at 10:06
NoobCoder
808
808
1
Use a regex to do this?
– triplem
Nov 19 at 10:11
add a comment |
1
Use a regex to do this?
– triplem
Nov 19 at 10:11
1
1
Use a regex to do this?
– triplem
Nov 19 at 10:11
Use a regex to do this?
– triplem
Nov 19 at 10:11
add a comment |
4 Answers
4
active
oldest
votes
up vote
2
down vote
You can find the second +
like so:
int firstPlus = email.indexOf('+');
int secondPlus = email.indexOf('+', firstPlus + 1);
(You need to handle the case that there aren't two +
s to find, if necessary).
Then find the following @
:
int at = email.indexOf('@', secondPlus);
Then stitch it back together:
String newEmail = email.substring(0, secondPlus + 1) + email.substring(at);
or
String newEmail2 = new StringBuilder(email).delete(secondPlus + 1, at).toString();
Ideone demo
add a comment |
up vote
1
down vote
Unfortunately Java doesn't have methods like replace second, replace third etc. You can either replaceAll (which will replace all occurences) OR invoce replaceFirst again on the already replaced string. That's basically replacing the second. If you want to replace ONLY the second - then you can do it with substrings or do a regex matcher and iterate on results.
public static void main(String args) {
String email = "alice+luna@john+smith@steve+oliver@";
String newEmail = email.replaceFirst("\+.*?@", "");
newEmail = newEmail .replaceFirst("\+.*?@", ""); //this replaces the second right? :)
newEmail = newEmail .replaceFirst("\+.*?@", ""); // replace 3rd etc.
System.out.println(newEmail);
}
add a comment |
up vote
0
down vote
You can alternate value of parameter n
in following replaceNth
method to 2, 3 to perform exactly the same operation as that of replaceSecond or replaceThird. ( Note: this method can be applied in any other value of n. If nth pattern do not exist , it simply return given string ).
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static String replaceNth(String str, int n, String regex, String replaceWith) {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
while (m.find()) {
n--;
if (n == 0) {
return str.substring(0,m.start() + 1)+ replaceWith + str.substring(m.end() - 1);
}
}
return str;
}
public static void main(String args) {
String email = "alice+luna@john+smith@steve+oliver@";
System.out.println(replaceNth(email, 2, "\+.*?@", ""));
}
}
add a comment |
up vote
0
down vote
I think it is better to encapsulate this logic into separate method, where String
and group position
are arguments.
private static final Pattern PATTERN = Pattern.compile("([^+@]+)@");
private static String removeSubstringGroup(String str, int pos) {
Matcher matcher = PATTERN.matcher(str);
while (matcher.find()) {
if (pos-- == 0)
return str.substring(0, matcher.start()) + str.substring(matcher.end() - 1);
}
return str;
}
Additionally, you can add more methods, to simplify using this util; like removeFirst()
or removeLast()
public static String removeFirst(String str) {
return removeSubstringGroup(str, 0);
}
public static String removeSecond(String str) {
return removeSubstringGroup(str, 1);
}
Demo:
String email = "alice+luna@john+smith@steve+oliver@";
System.out.println(email);
System.out.println(removeFirst(email));
System.out.println(removeSecond(email));
System.out.println(removeSubstringGroup(email, 2));
System.out.println(removeSubstringGroup(email, 3));
Output:
alice+luna@john+smith@steve+oliver@
alice+@john+smith@steve+oliver@
alice+luna@john+@steve+oliver@
alice+luna@john+smith@steve+@
alice+luna@john+smith@steve+oliver@
Ideone demo
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
You can find the second +
like so:
int firstPlus = email.indexOf('+');
int secondPlus = email.indexOf('+', firstPlus + 1);
(You need to handle the case that there aren't two +
s to find, if necessary).
Then find the following @
:
int at = email.indexOf('@', secondPlus);
Then stitch it back together:
String newEmail = email.substring(0, secondPlus + 1) + email.substring(at);
or
String newEmail2 = new StringBuilder(email).delete(secondPlus + 1, at).toString();
Ideone demo
add a comment |
up vote
2
down vote
You can find the second +
like so:
int firstPlus = email.indexOf('+');
int secondPlus = email.indexOf('+', firstPlus + 1);
(You need to handle the case that there aren't two +
s to find, if necessary).
Then find the following @
:
int at = email.indexOf('@', secondPlus);
Then stitch it back together:
String newEmail = email.substring(0, secondPlus + 1) + email.substring(at);
or
String newEmail2 = new StringBuilder(email).delete(secondPlus + 1, at).toString();
Ideone demo
add a comment |
up vote
2
down vote
up vote
2
down vote
You can find the second +
like so:
int firstPlus = email.indexOf('+');
int secondPlus = email.indexOf('+', firstPlus + 1);
(You need to handle the case that there aren't two +
s to find, if necessary).
Then find the following @
:
int at = email.indexOf('@', secondPlus);
Then stitch it back together:
String newEmail = email.substring(0, secondPlus + 1) + email.substring(at);
or
String newEmail2 = new StringBuilder(email).delete(secondPlus + 1, at).toString();
Ideone demo
You can find the second +
like so:
int firstPlus = email.indexOf('+');
int secondPlus = email.indexOf('+', firstPlus + 1);
(You need to handle the case that there aren't two +
s to find, if necessary).
Then find the following @
:
int at = email.indexOf('@', secondPlus);
Then stitch it back together:
String newEmail = email.substring(0, secondPlus + 1) + email.substring(at);
or
String newEmail2 = new StringBuilder(email).delete(secondPlus + 1, at).toString();
Ideone demo
edited Nov 19 at 10:17
answered Nov 19 at 10:09
Andy Turner
79.1k878131
79.1k878131
add a comment |
add a comment |
up vote
1
down vote
Unfortunately Java doesn't have methods like replace second, replace third etc. You can either replaceAll (which will replace all occurences) OR invoce replaceFirst again on the already replaced string. That's basically replacing the second. If you want to replace ONLY the second - then you can do it with substrings or do a regex matcher and iterate on results.
public static void main(String args) {
String email = "alice+luna@john+smith@steve+oliver@";
String newEmail = email.replaceFirst("\+.*?@", "");
newEmail = newEmail .replaceFirst("\+.*?@", ""); //this replaces the second right? :)
newEmail = newEmail .replaceFirst("\+.*?@", ""); // replace 3rd etc.
System.out.println(newEmail);
}
add a comment |
up vote
1
down vote
Unfortunately Java doesn't have methods like replace second, replace third etc. You can either replaceAll (which will replace all occurences) OR invoce replaceFirst again on the already replaced string. That's basically replacing the second. If you want to replace ONLY the second - then you can do it with substrings or do a regex matcher and iterate on results.
public static void main(String args) {
String email = "alice+luna@john+smith@steve+oliver@";
String newEmail = email.replaceFirst("\+.*?@", "");
newEmail = newEmail .replaceFirst("\+.*?@", ""); //this replaces the second right? :)
newEmail = newEmail .replaceFirst("\+.*?@", ""); // replace 3rd etc.
System.out.println(newEmail);
}
add a comment |
up vote
1
down vote
up vote
1
down vote
Unfortunately Java doesn't have methods like replace second, replace third etc. You can either replaceAll (which will replace all occurences) OR invoce replaceFirst again on the already replaced string. That's basically replacing the second. If you want to replace ONLY the second - then you can do it with substrings or do a regex matcher and iterate on results.
public static void main(String args) {
String email = "alice+luna@john+smith@steve+oliver@";
String newEmail = email.replaceFirst("\+.*?@", "");
newEmail = newEmail .replaceFirst("\+.*?@", ""); //this replaces the second right? :)
newEmail = newEmail .replaceFirst("\+.*?@", ""); // replace 3rd etc.
System.out.println(newEmail);
}
Unfortunately Java doesn't have methods like replace second, replace third etc. You can either replaceAll (which will replace all occurences) OR invoce replaceFirst again on the already replaced string. That's basically replacing the second. If you want to replace ONLY the second - then you can do it with substrings or do a regex matcher and iterate on results.
public static void main(String args) {
String email = "alice+luna@john+smith@steve+oliver@";
String newEmail = email.replaceFirst("\+.*?@", "");
newEmail = newEmail .replaceFirst("\+.*?@", ""); //this replaces the second right? :)
newEmail = newEmail .replaceFirst("\+.*?@", ""); // replace 3rd etc.
System.out.println(newEmail);
}
answered Nov 19 at 10:11
Veselin Davidov
5,5661515
5,5661515
add a comment |
add a comment |
up vote
0
down vote
You can alternate value of parameter n
in following replaceNth
method to 2, 3 to perform exactly the same operation as that of replaceSecond or replaceThird. ( Note: this method can be applied in any other value of n. If nth pattern do not exist , it simply return given string ).
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static String replaceNth(String str, int n, String regex, String replaceWith) {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
while (m.find()) {
n--;
if (n == 0) {
return str.substring(0,m.start() + 1)+ replaceWith + str.substring(m.end() - 1);
}
}
return str;
}
public static void main(String args) {
String email = "alice+luna@john+smith@steve+oliver@";
System.out.println(replaceNth(email, 2, "\+.*?@", ""));
}
}
add a comment |
up vote
0
down vote
You can alternate value of parameter n
in following replaceNth
method to 2, 3 to perform exactly the same operation as that of replaceSecond or replaceThird. ( Note: this method can be applied in any other value of n. If nth pattern do not exist , it simply return given string ).
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static String replaceNth(String str, int n, String regex, String replaceWith) {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
while (m.find()) {
n--;
if (n == 0) {
return str.substring(0,m.start() + 1)+ replaceWith + str.substring(m.end() - 1);
}
}
return str;
}
public static void main(String args) {
String email = "alice+luna@john+smith@steve+oliver@";
System.out.println(replaceNth(email, 2, "\+.*?@", ""));
}
}
add a comment |
up vote
0
down vote
up vote
0
down vote
You can alternate value of parameter n
in following replaceNth
method to 2, 3 to perform exactly the same operation as that of replaceSecond or replaceThird. ( Note: this method can be applied in any other value of n. If nth pattern do not exist , it simply return given string ).
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static String replaceNth(String str, int n, String regex, String replaceWith) {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
while (m.find()) {
n--;
if (n == 0) {
return str.substring(0,m.start() + 1)+ replaceWith + str.substring(m.end() - 1);
}
}
return str;
}
public static void main(String args) {
String email = "alice+luna@john+smith@steve+oliver@";
System.out.println(replaceNth(email, 2, "\+.*?@", ""));
}
}
You can alternate value of parameter n
in following replaceNth
method to 2, 3 to perform exactly the same operation as that of replaceSecond or replaceThird. ( Note: this method can be applied in any other value of n. If nth pattern do not exist , it simply return given string ).
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static String replaceNth(String str, int n, String regex, String replaceWith) {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
while (m.find()) {
n--;
if (n == 0) {
return str.substring(0,m.start() + 1)+ replaceWith + str.substring(m.end() - 1);
}
}
return str;
}
public static void main(String args) {
String email = "alice+luna@john+smith@steve+oliver@";
System.out.println(replaceNth(email, 2, "\+.*?@", ""));
}
}
answered Nov 19 at 10:47
Bishal Gautam
419416
419416
add a comment |
add a comment |
up vote
0
down vote
I think it is better to encapsulate this logic into separate method, where String
and group position
are arguments.
private static final Pattern PATTERN = Pattern.compile("([^+@]+)@");
private static String removeSubstringGroup(String str, int pos) {
Matcher matcher = PATTERN.matcher(str);
while (matcher.find()) {
if (pos-- == 0)
return str.substring(0, matcher.start()) + str.substring(matcher.end() - 1);
}
return str;
}
Additionally, you can add more methods, to simplify using this util; like removeFirst()
or removeLast()
public static String removeFirst(String str) {
return removeSubstringGroup(str, 0);
}
public static String removeSecond(String str) {
return removeSubstringGroup(str, 1);
}
Demo:
String email = "alice+luna@john+smith@steve+oliver@";
System.out.println(email);
System.out.println(removeFirst(email));
System.out.println(removeSecond(email));
System.out.println(removeSubstringGroup(email, 2));
System.out.println(removeSubstringGroup(email, 3));
Output:
alice+luna@john+smith@steve+oliver@
alice+@john+smith@steve+oliver@
alice+luna@john+@steve+oliver@
alice+luna@john+smith@steve+@
alice+luna@john+smith@steve+oliver@
Ideone demo
add a comment |
up vote
0
down vote
I think it is better to encapsulate this logic into separate method, where String
and group position
are arguments.
private static final Pattern PATTERN = Pattern.compile("([^+@]+)@");
private static String removeSubstringGroup(String str, int pos) {
Matcher matcher = PATTERN.matcher(str);
while (matcher.find()) {
if (pos-- == 0)
return str.substring(0, matcher.start()) + str.substring(matcher.end() - 1);
}
return str;
}
Additionally, you can add more methods, to simplify using this util; like removeFirst()
or removeLast()
public static String removeFirst(String str) {
return removeSubstringGroup(str, 0);
}
public static String removeSecond(String str) {
return removeSubstringGroup(str, 1);
}
Demo:
String email = "alice+luna@john+smith@steve+oliver@";
System.out.println(email);
System.out.println(removeFirst(email));
System.out.println(removeSecond(email));
System.out.println(removeSubstringGroup(email, 2));
System.out.println(removeSubstringGroup(email, 3));
Output:
alice+luna@john+smith@steve+oliver@
alice+@john+smith@steve+oliver@
alice+luna@john+@steve+oliver@
alice+luna@john+smith@steve+@
alice+luna@john+smith@steve+oliver@
Ideone demo
add a comment |
up vote
0
down vote
up vote
0
down vote
I think it is better to encapsulate this logic into separate method, where String
and group position
are arguments.
private static final Pattern PATTERN = Pattern.compile("([^+@]+)@");
private static String removeSubstringGroup(String str, int pos) {
Matcher matcher = PATTERN.matcher(str);
while (matcher.find()) {
if (pos-- == 0)
return str.substring(0, matcher.start()) + str.substring(matcher.end() - 1);
}
return str;
}
Additionally, you can add more methods, to simplify using this util; like removeFirst()
or removeLast()
public static String removeFirst(String str) {
return removeSubstringGroup(str, 0);
}
public static String removeSecond(String str) {
return removeSubstringGroup(str, 1);
}
Demo:
String email = "alice+luna@john+smith@steve+oliver@";
System.out.println(email);
System.out.println(removeFirst(email));
System.out.println(removeSecond(email));
System.out.println(removeSubstringGroup(email, 2));
System.out.println(removeSubstringGroup(email, 3));
Output:
alice+luna@john+smith@steve+oliver@
alice+@john+smith@steve+oliver@
alice+luna@john+@steve+oliver@
alice+luna@john+smith@steve+@
alice+luna@john+smith@steve+oliver@
Ideone demo
I think it is better to encapsulate this logic into separate method, where String
and group position
are arguments.
private static final Pattern PATTERN = Pattern.compile("([^+@]+)@");
private static String removeSubstringGroup(String str, int pos) {
Matcher matcher = PATTERN.matcher(str);
while (matcher.find()) {
if (pos-- == 0)
return str.substring(0, matcher.start()) + str.substring(matcher.end() - 1);
}
return str;
}
Additionally, you can add more methods, to simplify using this util; like removeFirst()
or removeLast()
public static String removeFirst(String str) {
return removeSubstringGroup(str, 0);
}
public static String removeSecond(String str) {
return removeSubstringGroup(str, 1);
}
Demo:
String email = "alice+luna@john+smith@steve+oliver@";
System.out.println(email);
System.out.println(removeFirst(email));
System.out.println(removeSecond(email));
System.out.println(removeSubstringGroup(email, 2));
System.out.println(removeSubstringGroup(email, 3));
Output:
alice+luna@john+smith@steve+oliver@
alice+@john+smith@steve+oliver@
alice+luna@john+@steve+oliver@
alice+luna@john+smith@steve+@
alice+luna@john+smith@steve+oliver@
Ideone demo
edited Nov 19 at 11:07
answered Nov 19 at 10:50
oleg.cherednik
4,6172916
4,6172916
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372287%2fwhat-is-equivalent-to-replacesecond-or-replacethird%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Use a regex to do this?
– triplem
Nov 19 at 10:11