Java程序删除句子的单词

时间:2020-02-23 14:35:07  来源:igfitidea点击:

在本教程中,我们将看到如何从句子中删除单词。
我们只需使用String的替换方法将单词用空字符串("")替换。
这是简单的Java程序,可以从句子中删除单词。

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

输出:

Enter a String : theitroad is a technical blog.
Enter a Word you want to Delete from the String : technicalNew String is : theitroad is a blog.