Java replace () проблемы
Я должен ввести строку и заменить всеand
, to
, you
, а такжеfor
подстроки с&
, 2
, U
, а также4
.
Когда я ввожу строку"and , and,and , to , to,to , you ,you , you, for ,for , for,a , a,e , e,i , i,o , o,u , u"
он только выводитand
когда я распечатал это.
public void simplify()
{
System.out.println("Enter a string to simplify: ");
String rope = in.next();
System.out.println(simplifier(rope));
}
public String simplifier(String rope)
{
rope = rope.replace(" and "," & ");
rope = rope.replace(" and"," &");
rope = rope.replace("and ","& ");
rope = rope.replace(" to "," 2 ");
rope = rope.replace(" to"," 2");
rope = rope.replace("to ","2 ");
rope = rope.replace(" you "," U ");
rope = rope.replace("you ","U ");
rope = rope.replace(" you"," U");
rope = rope.replace(" for "," 4 ");
rope = rope.replace("for ","4 ");
rope = rope.replace(" for"," 4");
rope = rope.replace("a ","");
rope = rope.replace(" a","");
rope = rope.replace("e ","");
rope = rope.replace(" e","");
rope = rope.replace("i ","");
rope = rope.replace(" i","");
rope = rope.replace(" o","");
rope = rope.replace("o ","");
rope = rope.replace("u ","");
rope = rope.replace(" u","");
System.out.print(rope);
return rope;
}
Выход:and and
Кажется, обрезать возвращенную строку после первого пробела
Я понятия не имею, что происходит и почему это не работает, как следует. Что я делаю неправильно?