删除字符串中的某些字符,Java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13229157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:58:26  来源:igfitidea点击:

Removing Certain Characters inside a String, Java

javaandroidregexstring

提问by Christian Eric Paran

My problem here is that i want a Character remove in some parts of a String but I do not know how to restrict the removing.

我的问题是我想在字符串的某些部分删除一个字符,但我不知道如何限制删除。

Example:

例子:

A computer is a general purpose device that can be\n
programmed to carry out a finite set of\n 
millions to billions of times more capable.\n
\n
In this era mechanical analog computers were used\n
for military applications.\n
1.1 Limited-function early computers\n
1.2 First general-purpose computers\n
1.3 Stored-program architecture\n
1.4 Semiconductors and\n

this here example is the content of my string, what i want to happen is to remove the \nof lines 1 and 2 above but not to remove the \nin line 5 onwards. How do i remove the \nwithout removing the other \n?. My Goal here is to make the string a paragraph without \nafter line. like the example the first 3 lines can be a paragraph and the next lines are in bullet form(example). what i am saying is that I do not want to remove \nin bulleted characters.

这里的示例是我的字符串的内容,我想要发生的是删除\n上面的第 1 行和第 2 行,但不删除第\n5 行以后的内容。我如何删除\n而不删除另一个\n. 我的目标是使字符串成为没有\n行后的段落。像示例一样,前 3 行可以是一个段落,下一行是项目符号形式(示例)。我的意思是我不想删除\n项目符号字符。

The real contents of the string is dynamic.

字符串的真实内容是动态的。

I have tried using String.replaceAll("\n", " ")well clearly that would not work it will remove all the \ni have thought of using Regexto determine what is Alphanumberic but it would remove some letters after \n

我已经尝试过String.replaceAll("\n", " ")清楚地使用它是行不通的,它会删除\n我想Regex用来确定什么是字母数字的所有内容,但它会在之后删除一些字母\n

回答by Rohit Jain

Try using this regex: -

尝试使用此正则表达式:-

str = str.replaceAll("(.+)(?<!\.)\n(?!\d)", " ");
System.out.println(str);

This will replace your \nif it is not precededby a dot - termination of a paragraph, and it is not followedby a digit, for when it is followed by a bulleted point. (like, your \nin first bullet point is followed by a 1.2. So, it will not be replaced.).

这将替换你的,\n如果它是not preceded由 a代替的dot - termination of a paragraph,它是not followed由 a digit,因为它后面是一个项目符号。(例如,您\n的第一个项目符号后跟一个1.2。因此,它不会被替换。)。

  • (.+)at the start, ensures that you are not replacing a blank line.
  • (.+)在开始时,确保您不会替换blank line.

This will work for the string you have shown.

这将适用于您显示的字符串。

Explanation: -

解释: -

  • (.+)-> A capture group, capturing anything, occurring at least once.

  • (?<!\\.)-> This is called negative-look-behind. It matches the stringfollowing it, only if that string is not preceded by a dot(.)given in the negative-look-behindpattern.
    For e.g.: -You don't need to replace \nafter the line: - millions to billions of times more capable.\n.

  • (?!\\d)-> This is called negative -look-ahead. It matches string behind it, only if that string is not followed by a digit (\\d)given in the negative-look-aheadpattern.
    For e.g.: -In your bulleted points, computers\nis followed by 1.2. where 1is a digit. So, you don't want to replace that \n.

  • Now, $1and $2represent the groups captured in the pattern match. Since you just want to replace "\n". So, we took the remaining pattern match as it is, while replacing "\n"with a space.

  • (.+)-> 一个捕获组,捕获任何东西,至少发生一次。

  • (?<!\\.)-> 这叫做negative-look-behind. 它匹配string后面的它,仅当该字符串之前没有在模式中dot(.)给定negative-look-behind
    例如: -您不需要\n在行之后替换: - millions to billions of times more capable.\n

  • (?!\\d)-> 这叫做negative -look-ahead. 它匹配后面的字符串,仅当该字符串后面没有模式中的digit (\\d)给定negative-look-ahead
    例如: -在您的项目符号中,computers\n后跟1.2. 哪里1是数字。所以,你不想替换那个\n

  • 现在,$1$2表示在模式匹配捕获的组。由于您只想替换"\n". 因此,我们按原样采用剩余的模式匹配,同时替换"\n"space.

So, $1is representation for 1st group- (.+)

所以,$1是代表1st group-(.+)

Note, look-aheadand look-behindregexes are non-capturinggroups.

注意,look-aheadlook-behind正则表达式的non-capturing群体。

For More Details, follow these links: -

有关更多详细信息,请访问以下链接:-

回答by Vincent Jia

I suspect your requirement is to remove the \n of lines 1 and 2 .
What you can do is as below:

我怀疑您的要求是删除第 1 行和第 2 行的 \n 。
你可以做的是如下:

  • split your string into segments,

  • String[] array = yourString.split("\n");

  • concat every segments by adding \n tag, except line 1,2

  • array[1] + array[2] + array[3] + '\n' + array[4] + '\n' ...// and so forth

  • 将你的字符串分成几段,

  • String[] array = yourString.split("\n");

  • 通过添加 \n 标记连接每个段,第 1,2 行除外

  • 数组[1] + 数组[2] + 数组[3] + '\n' + 数组[4] + '\n' ...// 等等