2 回答

TA貢獻(xiàn)1995條經(jīng)驗(yàn) 獲得超2個(gè)贊
你可以分開。并獲取最后一個(gè)索引,然后像這樣插入子字符串
String sentence ="Meow test test hello test.";
String[] temp = sentence.split("[|!|\\?|.|\\s|\\n]");
String word = temp[temp.length-1];
int index = sentence.lastIndexOf(word);
String out = sentence.substring(0,index) + " INSERTED WORD" + sentence.substring(index+word.length(), sentence.length());

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超8個(gè)贊
您可以使用像這樣的正則表達(dá)式(\w+.?)$,它會(huì)匹配字符串的最后一個(gè)單詞,即使它以..
String sentence = "I am a full sentence";
String replaced = sentence.replaceAll("(\\w+.?)$", "replaced");
System.out.println(replaced); // prints 'I am a full replaced'
添加回答
舉報(bào)