Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 删除字符串中最后一个已知的单词_Java_String - Fatal编程技术网

Java 删除字符串中最后一个已知的单词

Java 删除字符串中最后一个已知的单词,java,string,Java,String,如何删除字符串中最后一个已知的单词 比如这里的一句话, “你好,世界。更多信息> 我想删除最后一个单词more>>。这个单词在我所有的字符串集合中都是已知的 尝试使用replaceAll方法,但无法使其工作 我还尝试了子字符串,如下所示: person=person.substring(0,person.lastIndexOf(“”)+“”; 但是它删除了我句子中的最后两个单词。regex有帮助吗?使用regex replaceall捕获组非常简单。 请参见您的示例: System.o

如何删除字符串中最后一个已知的单词

比如这里的一句话,

“你好,世界。更多信息>

我想删除最后一个单词
more>>
。这个单词在我所有的字符串集合中都是已知的

尝试使用
replaceAll
方法,但无法使其工作

我还尝试了
子字符串
,如下所示:

person=person.substring(0,person.lastIndexOf(“”)+“”;


但是它删除了我句子中的最后两个单词。regex有帮助吗?

使用regex replaceall捕获组非常简单。 请参见您的示例:

     System.out.println("Hello, World. more>>".replaceAll("(.+)\\s+\\S+$", "$1"));
输出:

     "Hello, World."
如果你想要一些解释,你可以问我这个正则表达式是如何工作的

再次阅读..您可以直接使用以下代码:

     System.out.println("Hello, World. more>>".replaceAll("(.+)\\s+more>>$", "$1"));

注意,正如您所说,在您的所有字符串集合中都会出现“more>>”。

如果我理解您的问题,您可以编写一个类似
String removeFromEnd(String,String)
-

哪个输出

'Hello, World.'
我相信你正在寻找的是这样的东西。从结尾开始递减,直到你找到空间,然后看看这是否是一个已知词,如果不是,那么继续递减 直到你找到它。如果你找不到,就把原件还给我

但是,如果您必须从大量字符串中进行检查,我建议使用设置的数据结构来存储所有唯一的字符串,以便您可以访问它们O(1)。这将确保在调用此方法时运行时间为O(n)

尝试使用replaceAll方法,但无法使其工作

replaceAll方法采用正则表达式模式,这可能会让您感到困惑。该方法的签名如下:

public String replaceAll (String regularExpression, String replacement)
replace
方法应该可以工作,还可以替换指定字符串的所有匹配项

public String replace (CharSequence target, CharSequence replacement) 
Copies this string replacing occurrences of the specified target sequence with another sequence. The string is processed from the beginning to the end.
public int lastIndexOf(字符串)


也应该可以。确保搜索字符串实际匹配(可能空格不是真正的空格,等等)。

尝试使用“用单词边界替换全部”。这应该可以

String abc = "Hello, World. more>>";
String x = ">>";
System.out.println(abc);
abc= abc.replaceAll("\\bmore\\b"+x,"");
System.out.println(abc);

假设每个单词与其他单词之间用“”分隔,则:

公共字符串getWithoutLast(字符串MyPhase){
字符串结果=”;
字符串[]temp=myPhrase.trim().split(“”);
//循环从第一个单词到最后一个单词之前的单词
结果=温度[0];

对于(int i=1;ii如果要删除最后两个单词,
(空格)实际上可能不是分隔符,它可能是一个“空白”字符(如ASCII 255)如何在字符串中写入ASCII?它是
&255;
?我要做的第一件事是确定这是否是事实。打印
字符串的每个字符
,然后使用
(int)c
(其中c是
字符
)并查看最后一个空白字符实际是什么(如果打印字符本身也很有用;)
public String replace (CharSequence target, CharSequence replacement) 
Copies this string replacing occurrences of the specified target sequence with another sequence. The string is processed from the beginning to the end.
String abc = "Hello, World. more>>";
String x = ">>";
System.out.println(abc);
abc= abc.replaceAll("\\bmore\\b"+x,"");
System.out.println(abc);
public String getWithoutLast(String myPhrase) {

    String result = "";
    String [] temp = myPhrase.trim().split(" ");

    //Cycle going from the first word to the word before the last word
    result = temp[0];
    for (int i=1; i<temp.length-1; i++){
        result += " " + temp[i];
    }

    return result;
}