简体   繁体   English

如何删除句子中的一些单词Java?

[英]How to remove some word in a sentences Java?

Assume the string is: 假设字符串是:

The/at Fulton/np-tl County/nn-tl Grand/jj-tl

How can I remove character after / and the out put as below The Fulton County Grand 如何The Fulton County Grand之后删除/和输出后的字符

It looks like a simple regex-based replace could work fine here: 看起来像一个简单的基于正则表达式的替换可以在这里正常工作:

text = text.replaceAll("/\\S*", "");

Here the \\\\S* means "0 or more non-whitespace characters". 这里\\\\S*表示“0个或更多非空白字符”。 There are other options you could use too, of course. 当然,您还可以使用其他选项。

String input = "The/at Fulton/np-tl County/nn-tl Grand/jj-tl";
String clean = input.replaceAll("/.*?(?= |$)", "");

Here's a test: 这是一个测试:

public static void main( String[] args ) {
    String input = "The/at Fulton/np-tl County/nn-tl Grand/jj-tl";
    String clean = input.replaceAll("/.*?(?= |$)", "");
    System.out.println( clean);
}

Output: 输出:

The Fulton County Grand
String text = "The/at Fulton/np-tl County/nn-tl Grand/jj-tl";
String newText = text.replaceAll("/.*?\\S*", "");

From Java API: 来自Java API:

String  replace(char oldChar, char newChar)
Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.

String  replace(CharSequence target, CharSequence replacement)
Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.

String  replaceAll(String regex, String replacement)
Replaces each substring of this string that matches the given regular expression with the given replacement.

String  replaceFirst(String regex, String replacement)
Replaces the first substring of this string that matches the given regular expression with the given replacement.

If you need to replace a substring or a character, use 1st 2 methods. 如果需要替换子字符串或字符,请使用前2个方法。 If you need to replace a pattern or a regex, used 2nd 2 methods. 如果您需要替换模式或正则表达式,请使用第2种方法。

do as follow: 做如下:

startchar : is a starting character from which you want to replace. startchar :是要替换的起始字符。

endchar : is a ending character up to chich character you want to replace. endchar :是要替换的chich字符的结束字符。

" " : is because you just want to delete it so replace with white space “” :是因为你只想删除它,所以用空格替换

string.replaceAll(startchar+".*"+endchar, "")

refer http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#replaceAll%28java.lang.String,%20java.lang.String%29 参考http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#replaceAll%28java.lang.String,%20java.lang.String%29

also see greedy quantifier examples 还看到贪婪的量词例子

see working example 看工作实例

 public static void main( String[] args ) {
        String startchar ="/";
        String endchar="?(\\s|$)";
    String input = "The/at Fulton/np-tl County/nn-tl Grand/jj-tl";
    String clean = input.replaceAll(startchar+".*"+endchar, " ");
    System.out.println( clean);
}

output 产量

The Fulton County Grand

This worked for me: 这对我有用:

String text = "The/at Fulton/np-tl County/nn-tl Grand/jj-tl";
String newText = text.replaceAll("/.*?(\\s|$)", " ").trim();

Yields: 产量:

The Fulton County Grand 富尔顿县大酒店

This basically replaces any character(s) which are after a / and are either followed by a white space or else, by the end of the string. 这基本上替换了在/之后的任何字符,或者后面跟着一个空格,或者在字符串的末尾。 The trim() at the end is to cater for the extra white space added by the replaceAll method. 最后的trim()是为了满足replaceAll方法添加的额外空白。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM