What exactly occurs when the while loop runs (what is done within the while loop)? (It's supposed to remove...












-1















 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










share|improve this question

























  • indexOf returns -1 when it doesn't find remove in cleaned.

    – Kevin Anderson
    Nov 16 '18 at 23:57






  • 2





    You may want to read up on how indexOf and substring work.

    – Kevin Anderson
    Nov 17 '18 at 0:15
















-1















 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










share|improve this question

























  • indexOf returns -1 when it doesn't find remove in cleaned.

    – Kevin Anderson
    Nov 16 '18 at 23:57






  • 2





    You may want to read up on how indexOf and substring work.

    – Kevin Anderson
    Nov 17 '18 at 0:15














-1












-1








-1








 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










share|improve this question
















 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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 find remove in cleaned.

    – Kevin Anderson
    Nov 16 '18 at 23:57






  • 2





    You may want to read up on how indexOf and substring 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






  • 2





    You may want to read up on how indexOf and substring 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












1 Answer
1






active

oldest

votes


















0














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;





share|improve this answer

























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    0














    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;





    share|improve this answer






























      0














      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;





      share|improve this answer




























        0












        0








        0







        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;





        share|improve this answer















        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;






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 17 '18 at 0:20

























        answered Nov 17 '18 at 0:12









        JereJere

        531218




        531218






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith