What exactly occurs when the while loop runs (what is done within the while loop)? (It's supposed to remove...
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 |
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
indexOf
returns -1 when it doesn't findremove
incleaned
.
– Kevin Anderson
Nov 16 '18 at 23:57
2
You may want to read up on howindexOf
andsubstring
work.
– Kevin Anderson
Nov 17 '18 at 0:15
add a comment |
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 '18 at 23:32
Jere
531218
531218
asked Nov 16 '18 at 23:54
javanoobjavanoob
12
12
indexOf
returns -1 when it doesn't findremove
incleaned
.
– Kevin Anderson
Nov 16 '18 at 23:57
2
You may want to read up on howindexOf
andsubstring
work.
– Kevin Anderson
Nov 17 '18 at 0:15
add a comment |
indexOf
returns -1 when it doesn't findremove
incleaned
.
– Kevin Anderson
Nov 16 '18 at 23:57
2
You may want to read up on howindexOf
andsubstring
work.
– Kevin Anderson
Nov 17 '18 at 0:15
indexOf
returns -1 when it doesn't find remove
in cleaned
.– Kevin Anderson
Nov 16 '18 at 23:57
indexOf
returns -1 when it doesn't find remove
in cleaned
.– Kevin Anderson
Nov 16 '18 at 23:57
2
2
You may want to read up on how
indexOf
and substring
work.– Kevin Anderson
Nov 17 '18 at 0:15
You may want to read up on how
indexOf
and substring
work.– Kevin Anderson
Nov 17 '18 at 0:15
add a comment |
1 Answer
1
active
oldest
votes
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 |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 |
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 |
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 '18 at 0:20
answered Nov 17 '18 at 0:12
JereJere
531218
531218
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.
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
indexOf
returns -1 when it doesn't findremove
incleaned
.– Kevin Anderson
Nov 16 '18 at 23:57
2
You may want to read up on how
indexOf
andsubstring
work.– Kevin Anderson
Nov 17 '18 at 0:15