In this post, we will see how to delete words from the sentence.
You can just use String’s replaceAll method to replace the word with empty String("").
Here is simple java program to delete words from the sentence.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.Scanner; public class StringOperator { public static void main(String args[]) { String str, word; Scanner scan = new Scanner(System.in); System.out.print("Enter a String : "); str = scan.nextLine(); System.out.print("Enter a Word you want to Delete from the String : "); word = scan.nextLine(); str = str.replaceAll(word, ""); System.out.print("\nNew String is : "); System.out.print(str); } } |
Output:
Enter a String : Java2Blog is a technical blog.
Enter a Word you want to Delete from the String : technicalNew String is : Java2Blog is a blog.
Enter a Word you want to Delete from the String : technicalNew String is : Java2Blog is a blog.
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.