replaceall

replace a char in String

被刻印的时光 ゝ 提交于 2019-12-02 10:15:44
Hi I'd like to replace a char in a String. My problem is that at first you don't know which char it is, so in some cases I get an error message when my char is for example '+'. I don't want my char being interpreted as regex, so what should I do? May code should be something like this: String test = "something"; char ca = input.chatAt(0); input = input.replaceAll("" + ca, ""); I hope you can help me. Just don't use regex then. input = input.replace(String.valueOf(ca), ""); The replaceAll method of String takes the String representation of a regular expression as an argument. The replace method

Java String.replaceAll() refer the latest found group

我只是一个虾纸丫 提交于 2019-12-02 08:43:33
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 . s.replaceAll("(a+)", "$1\n") should work: jshell> String s = "aaabbbaa" s ==> "aaabbbaa" jshell> s.replaceAll("(a+)", "$1\n") $2 ==> "aaa\nbbbaa\n" As pointed out in the comments already, you'll have to mark the capture group in

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

大憨熊 提交于 2019-12-02 07:50:53
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? Just use String.replace(). It functions the same way, but it deals with escaping the special characters internally to avoid you having to worry about regex. Documentation You can match literally. For instance, if we want to match

Java - replace() method using values from arrays is changing the array values?

允我心安 提交于 2019-12-02 07:39:21
I'm doing something like public static String[] list = {"a","b","c","d",} //It gives me a NullPointeException if I didn't use static public String encrypt(String a){ a = a.replace(list[0],list[2]); a = a.replace(list[4],list[3]); return a; } and I have another method that just reverses it public String decrypt(String a){ a = a.replace(list[2],list[0]); a = a.replace(list[3],list[4]); return a; } Of course this is simplified, the real code I'm using uses the entire alphabet and some numbers. So here's my problem: If I input something like 123 into encrypt() and it outputs ngV then I input ngV

How to not replace when preceded with some characters using String's replaceAll

被刻印的时光 ゝ 提交于 2019-12-02 06:45:59
问题 I need to replace some words in a text, but I need to put conditions in the replacement strategy as follows: I want to replace word1 with word2 : String word1 = "word1"; String word2 = "word2"; but I don't want to replace word1 if it's preceded by word3 which is: String word3 = "word3."; //with the dot at the ending That is if the text is word3.word1 I don't want to touch it. But I can't seem to handle that with word boundaries using String 's replaceAll method. EDIT: And also I don't want to

How can I perform case-insensitive pattern search and case-preserving replacement?

僤鯓⒐⒋嵵緔 提交于 2019-12-02 05:57:17
问题 Here is the scenario. String strText = "ABC abc Abc aBC abC aBc ABc AbC"; // Adding a HTML content to this String searchText = "abc"; String strFormatted = strText.replaceAll( "(?i)" + searchText, "<font color='red'>" + searchText + "</font>"); This returns a string with all the words in lower case and of course in red color. My requirement is to get the strFormatted as a String with the case same as Original String but it should have the Font tag. Is it possible to do this ? 回答1: You can use

Replace specific string by another - String#replaceAll()

时光总嘲笑我的痴心妄想 提交于 2019-12-02 04:22:18
I'm actually developping a parser and I'm stuck on a method. I need to clean specifics words in some sentences, meaning replacing those by a whitespace or a null character. For now, I came up with this code: private void clean(String sentence) { try { FileInputStream fis = new FileInputStream( ConfigHandler.getDefault(DictionaryType.CLEANING).getDictionaryFile()); BufferedReader bis = new BufferedReader(new InputStreamReader(fis)); String read; List<String> wordList = new ArrayList<String>(); while ((read = bis.readLine()) != null) { wordList.add(read); } } catch (IOException e) { e

How can I perform case-insensitive pattern search and case-preserving replacement?

穿精又带淫゛_ 提交于 2019-12-02 01:21:45
Here is the scenario. String strText = "ABC abc Abc aBC abC aBc ABc AbC"; // Adding a HTML content to this String searchText = "abc"; String strFormatted = strText.replaceAll( "(?i)" + searchText, "<font color='red'>" + searchText + "</font>"); This returns a string with all the words in lower case and of course in red color. My requirement is to get the strFormatted as a String with the case same as Original String but it should have the Font tag. Is it possible to do this ? You can use a backreference. Something like: String strFormatted = strText.replaceAll( "(?i)(" + searchText + ")", "

Leave a cell blank if condition is false

断了今生、忘了曾经 提交于 2019-12-01 07:21:25
问题 Other questions have touched on this and offered solutions that are not viable for a very large data set. I have a formula like the following across 9 columns: =IF(A1=A2, B2, "zz") I then autofill about 3.5 million cells + copy -> paste values. Then I find and replace "zz" with "empty cell". However, finding and replacing a million or so "zz" strings is a very slow process. I'd rather not write anything there in the first place. So my question is, how to I write the following formula: =IF(A1

java new line replacement

不想你离开。 提交于 2019-12-01 06:55:42
I am wondering about why I don't get the expected result with this one: String t = "1302248663033 <script language='javascript'>nvieor\ngnroeignrieogi</script>"; t.replaceAll("\n", ""); System.out.println(t); The output is: 1302248663033 <script language='javascript'>nvieor gnroeignrieogi</script> So I am wondering why \n is still there. Anybody knows? Is \n special in someway? EDIT: So I was having trouble with matching the newline character with a . in a regex expression, not realizing that one use to use the DOTALL option, so I'll add what one needs to do here for future reference: String