replaceall

Strange Behaviour of replaceAll method of String

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 07:35:55
问题 I am facing strange behaviour of replaceAll method of String class. I have a string buffer which contain below data keyRPT1={keyRPT11=01|keyRPT19=01}|keyRPT3={keyRPT11=03|keyRPT19=01}|keyRPT8={keyRPT11=08|keyRPT19=01} i write below code to replace the "keyRPT11=08|keyRPT19=01" with "keyRPT11=08|keyRPT19=2" i am using below code for that String complementaryInformation = "keyRPT1={keyRPT11=01|keyRPT19=01}|keyRPT3={keyRPT11=03|keyRPT19=01}|keyRPT8={keyRPT11=08|keyRPT19=01}";

Java: Remove numbers from string

人走茶凉 提交于 2019-12-11 06:52:13
问题 I have a string like 23.Piano+trompet , and i wanted to remove the 23. part from the string using this function: private String removeSignsFromName(String name) { name = name.replaceAll(" ", ""); name = name.replaceAll(".", ""); return name.replaceAll("\\^([0-9]+)", ""); } But it doesn't do it. Also, there is no error in runtime. 回答1: The following replaces all whitespace characters ( \\s ), dots ( \\. ), and digits ( \\d ) with "" : name.replaceAll("^[\\s\\.\\d]+", ""); what if I want to

How to replace the characher `\n` as a new line in android

房东的猫 提交于 2019-12-10 13:07:42
问题 I have one server response for an API request as shown below. Success! Your request has been sent.\n\nWe’ll inform you once it is done. This message I need to show in a Snackbar. I need new line to be added in the place of \n in this response . I tried by using replaceAll like String message = (serverResponse.getMessage()).replaceAll("\\n", System.getProperty("line.separator")); but it is showing like this Same message if I add in string.xml resource file and get using getString(R.string

Java string replaceAll()

蹲街弑〆低调 提交于 2019-12-10 10:06:31
问题 I've been wondering if for example: JTextPane chatTextArea = new JTextPane(); s.replaceAll(":\\)", emoticon()); public String emoticon(){ chatTextArea.insertIcon(new ImageIcon(ChatFrame.class.getResource("/smile.png"))); return "`"; } can put a picture and a "`" everywhere ":)" is found. When I run it like this if s contains a ":)" then the whole s gets replaced just by the icon. Is there a way to do it? 回答1: Here is a small example I made (+1 to @StanislavL for the original), simply uses

Java string replaceAll()

六眼飞鱼酱① 提交于 2019-12-06 01:59:49
I've been wondering if for example: JTextPane chatTextArea = new JTextPane(); s.replaceAll(":\\)", emoticon()); public String emoticon(){ chatTextArea.insertIcon(new ImageIcon(ChatFrame.class.getResource("/smile.png"))); return "`"; } can put a picture and a "`" everywhere ":)" is found. When I run it like this if s contains a ":)" then the whole s gets replaced just by the icon. Is there a way to do it? David Kroukamp Here is a small example I made (+1 to @StanislavL for the original), simply uses DocumentListener and checks when a matching sequence for an emoticon is entered and replaces it

replace() and replaceAll() in Java

萝らか妹 提交于 2019-12-05 22:20:31
问题 The following code uses the replace() method of the String class in Java. String a = "abc/xyz"; System.out.println(a.replace("/", "\\")); / in the given String a is being replaced with \ . The same thing is wrong, if we use the replaceAll() method as follows. System.out.println(a.replaceAll("/", "\\")); It causes the exception java.lang.StringIndexOutOfBoundsException to be thrown. It requires two additional backslashes \ like the following, since replaceAll() uses a regex which is not the

How to preserve newlines while reading a file using stream - java 8

拈花ヽ惹草 提交于 2019-12-05 02:24:56
try (Stream<String> lines = Files.lines(targetFile)) { List<String> replacedContent = lines.map(line -> StringUtils.replaceEach(line,keys, values)) .parallel() .collect(Collectors.toList()); Files.write(targetFile, replacedContent); } I'm trying to replace multiple text patterns in each line of the file. But I'm observing that "\r\n"(byte equivalent 10 and 13) is being replaced with just "\r"(just 10) and my comparison tests are failing. I want to preserve the newlines as they are in the input file and don't want java to touch them. Could anyone suggest if there is a way to do this without

What is the Java equivalent to this preg_replace?

若如初见. 提交于 2019-12-04 12:13:43
问题 <?php $str = "word <a href=\"word\">word</word>word word"; $str = preg_replace("/word(?!([^<]+)?>)/i","repl",$str); echo $str; # repl <word word="word">repl</word> ?> source: http://pureform.wordpress.com/2008/01/04/matching-a-word-characters-outside-of-html-tags/ Unfortunality my project needs a semantic libs avaliable only for Java... // Thanks Celso 回答1: Use the String.replaceAll() method: class Test { public static void main(String[] args) { String str = "word <a href=\"word\">word</word

remove substring in a string without using regex (cannot use replaceAll)

放肆的年华 提交于 2019-12-04 05:57:01
问题 I need to remove some substrings in strings (in a large dataset). The substrings often contain special characters, like these: ., ^, /,... and replaceAll() would treat them as special characters for regex, such as a dot would match any character, which is not really what I want. Is there other functions to do the "replace" without treating the first argument as regex? 回答1: Just use String.replace(). It functions the same way, but it deals with escaping the special characters internally to

Java String.replaceAll() refer the latest found group

眉间皱痕 提交于 2019-12-04 05:40:29
问题 Javadoc says $1, $2, etc. can be used to reference the match groups, but how does one reference the latest found group in the replacement string when using String.replaceAll() ? I.e. there's a string "aaabbbaa" and a regex "a+" , and I want to be able to do something like s.replaceAll("a+", "$\n") to get "aaa\nbbbaa\n" , but Java gives me the Illegal group reference . 回答1: s.replaceAll("(a+)", "$1\n") should work: jshell> String s = "aaabbbaa" s ==> "aaabbbaa" jshell> s.replaceAll("(a+)", "$1