What exactly occurs when the while loop runs (what is done within the while loop)? (It's supposed to remove...
up vote
-1
down vote
favorite
public String removeStrings()
{
String cleaned = sentence; //sentence and remove are inputs
int loc = cleaned.indexOf(remove);
while (loc>-1) //need explanation on how this works
{
cleaned = cleaned.substring(0, loc-1)+cleaned.substring(loc+remove.length());
loc = cleaned.indexOf(remove);
}
return cleaned;
}
Example input sentence="xR-MxR-MHelloxR-M" and remove="R-M" //has to remove x as well in this case
https://github.com/AndrewWeiler/AndrewMac/blob/master/ACSWeiler/src/Lab09/StringRemover.java
java substring indexof
add a comment |
up vote
-1
down vote
favorite
public String removeStrings()
{
String cleaned = sentence; //sentence and remove are inputs
int loc = cleaned.indexOf(remove);
while (loc>-1) //need explanation on how this works
{
cleaned = cleaned.substring(0, loc-1)+cleaned.substring(loc+remove.length());
loc = cleaned.indexOf(remove);
}
return cleaned;
}
Example input sentence="xR-MxR-MHelloxR-M" and remove="R-M" //has to remove x as well in this case
https://github.com/AndrewWeiler/AndrewMac/blob/master/ACSWeiler/src/Lab09/StringRemover.java
java substring indexof
indexOfreturns -1 when it doesn't findremoveincleaned.
– Kevin Anderson
Nov 16 at 23:57
2
You may want to read up on howindexOfandsubstringwork.
– Kevin Anderson
Nov 17 at 0:15
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
public String removeStrings()
{
String cleaned = sentence; //sentence and remove are inputs
int loc = cleaned.indexOf(remove);
while (loc>-1) //need explanation on how this works
{
cleaned = cleaned.substring(0, loc-1)+cleaned.substring(loc+remove.length());
loc = cleaned.indexOf(remove);
}
return cleaned;
}
Example input sentence="xR-MxR-MHelloxR-M" and remove="R-M" //has to remove x as well in this case
https://github.com/AndrewWeiler/AndrewMac/blob/master/ACSWeiler/src/Lab09/StringRemover.java
java substring indexof
public String removeStrings()
{
String cleaned = sentence; //sentence and remove are inputs
int loc = cleaned.indexOf(remove);
while (loc>-1) //need explanation on how this works
{
cleaned = cleaned.substring(0, loc-1)+cleaned.substring(loc+remove.length());
loc = cleaned.indexOf(remove);
}
return cleaned;
}
Example input sentence="xR-MxR-MHelloxR-M" and remove="R-M" //has to remove x as well in this case
https://github.com/AndrewWeiler/AndrewMac/blob/master/ACSWeiler/src/Lab09/StringRemover.java
java substring indexof
java substring indexof
edited Nov 20 at 23:32
Jere
518218
518218
asked Nov 16 at 23:54
javanoob
12
12
indexOfreturns -1 when it doesn't findremoveincleaned.
– Kevin Anderson
Nov 16 at 23:57
2
You may want to read up on howindexOfandsubstringwork.
– Kevin Anderson
Nov 17 at 0:15
add a comment |
indexOfreturns -1 when it doesn't findremoveincleaned.
– Kevin Anderson
Nov 16 at 23:57
2
You may want to read up on howindexOfandsubstringwork.
– Kevin Anderson
Nov 17 at 0:15
indexOf returns -1 when it doesn't find remove in cleaned.– Kevin Anderson
Nov 16 at 23:57
indexOf returns -1 when it doesn't find remove in cleaned.– Kevin Anderson
Nov 16 at 23:57
2
2
You may want to read up on how
indexOf and substring work.– Kevin Anderson
Nov 17 at 0:15
You may want to read up on how
indexOf and substring work.– Kevin Anderson
Nov 17 at 0:15
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
String.substring() either returns the part ob a String between two positions (in cleaned.substring(0, 1), that would be the content of cleaned between position 0 and 1) or if you give that method just 1 int-argument, it returns the part of your String, that comes after that position.
For example, with sentence="xR-MxR-MHelloxR-M" and remove="R-M" you would get:
cleaned = "xR-MxR-MHelloxR-M"
loc = 1
So the while-loop would go like this:
cleaned.substring(0, loc-1) returns "x" and cleaned.substring(loc+remove.length()) returns "xR-MHelloxR-M". So cleaned = "xxR-MHelloxR-M". Then, loc becomes the position of the next occurrence of remove.
In words, your while loop removes every occurrence of the String remove out of the String sentence and saves the result in cleaned.
For substring() check https://www.javatpoint.com/substring.
Edit:
If you want the first character before your substring to be removed too, you just need to say
loc = cleaned.indexOf(remove) - 1;
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
String.substring() either returns the part ob a String between two positions (in cleaned.substring(0, 1), that would be the content of cleaned between position 0 and 1) or if you give that method just 1 int-argument, it returns the part of your String, that comes after that position.
For example, with sentence="xR-MxR-MHelloxR-M" and remove="R-M" you would get:
cleaned = "xR-MxR-MHelloxR-M"
loc = 1
So the while-loop would go like this:
cleaned.substring(0, loc-1) returns "x" and cleaned.substring(loc+remove.length()) returns "xR-MHelloxR-M". So cleaned = "xxR-MHelloxR-M". Then, loc becomes the position of the next occurrence of remove.
In words, your while loop removes every occurrence of the String remove out of the String sentence and saves the result in cleaned.
For substring() check https://www.javatpoint.com/substring.
Edit:
If you want the first character before your substring to be removed too, you just need to say
loc = cleaned.indexOf(remove) - 1;
add a comment |
up vote
0
down vote
accepted
String.substring() either returns the part ob a String between two positions (in cleaned.substring(0, 1), that would be the content of cleaned between position 0 and 1) or if you give that method just 1 int-argument, it returns the part of your String, that comes after that position.
For example, with sentence="xR-MxR-MHelloxR-M" and remove="R-M" you would get:
cleaned = "xR-MxR-MHelloxR-M"
loc = 1
So the while-loop would go like this:
cleaned.substring(0, loc-1) returns "x" and cleaned.substring(loc+remove.length()) returns "xR-MHelloxR-M". So cleaned = "xxR-MHelloxR-M". Then, loc becomes the position of the next occurrence of remove.
In words, your while loop removes every occurrence of the String remove out of the String sentence and saves the result in cleaned.
For substring() check https://www.javatpoint.com/substring.
Edit:
If you want the first character before your substring to be removed too, you just need to say
loc = cleaned.indexOf(remove) - 1;
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
String.substring() either returns the part ob a String between two positions (in cleaned.substring(0, 1), that would be the content of cleaned between position 0 and 1) or if you give that method just 1 int-argument, it returns the part of your String, that comes after that position.
For example, with sentence="xR-MxR-MHelloxR-M" and remove="R-M" you would get:
cleaned = "xR-MxR-MHelloxR-M"
loc = 1
So the while-loop would go like this:
cleaned.substring(0, loc-1) returns "x" and cleaned.substring(loc+remove.length()) returns "xR-MHelloxR-M". So cleaned = "xxR-MHelloxR-M". Then, loc becomes the position of the next occurrence of remove.
In words, your while loop removes every occurrence of the String remove out of the String sentence and saves the result in cleaned.
For substring() check https://www.javatpoint.com/substring.
Edit:
If you want the first character before your substring to be removed too, you just need to say
loc = cleaned.indexOf(remove) - 1;
String.substring() either returns the part ob a String between two positions (in cleaned.substring(0, 1), that would be the content of cleaned between position 0 and 1) or if you give that method just 1 int-argument, it returns the part of your String, that comes after that position.
For example, with sentence="xR-MxR-MHelloxR-M" and remove="R-M" you would get:
cleaned = "xR-MxR-MHelloxR-M"
loc = 1
So the while-loop would go like this:
cleaned.substring(0, loc-1) returns "x" and cleaned.substring(loc+remove.length()) returns "xR-MHelloxR-M". So cleaned = "xxR-MHelloxR-M". Then, loc becomes the position of the next occurrence of remove.
In words, your while loop removes every occurrence of the String remove out of the String sentence and saves the result in cleaned.
For substring() check https://www.javatpoint.com/substring.
Edit:
If you want the first character before your substring to be removed too, you just need to say
loc = cleaned.indexOf(remove) - 1;
edited Nov 17 at 0:20
answered Nov 17 at 0:12
Jere
518218
518218
add a comment |
add a comment |
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%2f53346857%2fwhat-exactly-occurs-when-the-while-loop-runs-what-is-done-within-the-while-loop%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
indexOfreturns -1 when it doesn't findremoveincleaned.– Kevin Anderson
Nov 16 at 23:57
2
You may want to read up on how
indexOfandsubstringwork.– Kevin Anderson
Nov 17 at 0:15