简体   繁体   English

从Java中\\\\之后的字符串中删除几个字符

[英]Remove few characters from a string after \\ in java

I want to remove only 5 characters from a string after the occurrence of "\\"(double back slash). 在出现“ \\”(双反斜杠)后,我只想从字符串中删除5个字符。 For example:- 例如:-

String s = ":zap:\\ufe0f:umbrella:\\ufe0f:snowflake:\\ufe0f:snowman:\\ufe0f:cyclone:" ;

The output should be : 输出应为:

:zap::umbrella::snowflake::snowman::cyclone:

So, in the above string the 5 letters after the \\ (double back slash) has been removed. 因此,在上面的字符串中,\\(双反斜杠)后的5个字母已被删除。 How can I achieve this ? 我该如何实现?

I tried some code but no luck. 我尝试了一些代码,但是没有运气。 :( :(

\️ is an unicode which represents a single character. \️是一个表示单个字符的unicode。

This code will remove any character but not of a word character or colon. 此代码将删除任何字符,但不会删除单词字符或冒号。

String s = ":zap:\ufe0f:umbrella:\ufe0f:snowflake:\ufe0f:snowman:\ufe0f:cyclone:" ;
System.out.println(s.replaceAll("[^\\w:]", ""));

Output: 输出:

:zap::umbrella::snowflake::snowman::cyclone:

You can try this Regex "/\\\\.{5}/g" with the replaceAll() method: 您可以使用replaceAll()方法尝试此正则表达式"/\\\\.{5}/g"

String s = ":zap:\ufe0f:umbrella:\ufe0f:snowflake:\ufe0f:snowman:\ufe0f:cyclone:" ;
s=s.replaceAll("/\\.{5}/g", "");

Here is a DEMO of the Regex . 这是正则表达式演示

it matches any character exactly 5 times after the \\ . 它与\\之后恰好5次匹配任何字符。

It gives this output: 它给出以下输出:

:zap:️:umbrella:️:snowflake:️:snowman:️:cyclone:

You can try The working Example here 您可以在这里尝试工作示例

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

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