사용자가 문자열 str에서 단어를 제거 할 수있게하려고합니다. 예를 들어, "Hello my name is john"을 입력하면 출력은 "Hello is john"이어야합니다. 어떻게해야합니까?문자열의 단어 - 제거하는 방법
import java.util.*;
class WS7Q2{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Please enter a sentence");
String str = in.nextLine();
int j;
String[] words = str.split(" ");
String firstTwo = words[0] + " " + words[1]; // first two words
String lastTwo = words[words.length - 2] + " " + words[words.length - 1];//last two words
System.out.println(str);
}
}
String.replace ...? – MadProgrammer
'str = str.replace ("my name", "");' –