1
votes

I have this String:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Ut enim adminim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat 55555 Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

In this string, there are three paragraphs, I need to find this subString: "55555" and delete from there to the next newline (end of the line). How could you do this?

3
I do not know how to use the features to look for a subString and delete up to end of lineManu
with such text formatting, it is not clearly where paragraphs are.Andrew Tobilko
now you can see this...Manu

3 Answers

1
votes

You may choose the needed part of the whole string and split it by \n. Then change the paragraph that needs to be corrected.

String[] strings = string.split("\n");
strings[1] = strings[1].substring(0, strings[1].indexOf("55555"));
1
votes

This is my solution:

String strings = string.substring(string.indexOf("55555"), string.indexOf("\n"));
1
votes

I made a program that will hopefully solve your problem. Note that the program takes multi-line input, so you have to enter a comma (,) to end the input. It'll first take the main string as input and then ask for the word to remove from it. It's a simple program: doesn't use the substring() function or other techniques as you are not acquainted with.

class ProgramTest{

    public static void main(String[] args){

        java.util.Scanner sc=new java.util.Scanner(System.in);
        String str1="", str2="", a="", w="", mystr="";

        System.out.println("Enter your string. Enter ',' to terminate input.");

        while(!(a=sc.nextLine()).equals(","))
            str1 += a+"\n";

        System.out.println("Enter word to remove");

        str2 = sc.nextLine();

        for(int i=0;i<str1.length();i++){
            if(str1.charAt(i)!=' '){
                w+=str1.charAt(i);
            }
            else{
                if(!(w.equals(str2)))
                mystr += w+" ";
                w="";
            }
        }
        System.out.println("String after removing "+str2+" is \n"+mystr);
    }
}