replaceall

replace a char in String

≡放荡痞女 提交于 2019-12-04 05:34:40
问题 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. 回答1: Just don't use regex then. input = input.replace(String.valueOf(ca), ""); The replaceAll

replace() and replaceAll() in Java

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 04:46:10
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 case with the replace() method. System.out.println(a.replaceAll("/", "\\\\")); The only question is why

java new line replacement

拟墨画扇 提交于 2019-12-04 01:38:28
问题 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

What is the Java equivalent to this preg_replace?

爱⌒轻易说出口 提交于 2019-12-03 07:48:34
<?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 Use the String.replaceAll() method: class Test { public static void main(String[] args) { String str = "word <a href=\"word\">word</word>word word"; str = str.replaceAll("word(?!([^<]+)?>)", "repl"); System.out.println(str); } } Hope this helps.

Replace all method throws PatternSyntaxException

大憨熊 提交于 2019-12-02 21:09:20
问题 look at the following code: String comment = "1)FCR pick up in Hong Kong2)Local charges will be paiy in Hong Kong & in HK$.3)Booking:virginiawong@fahkco.com.hk4)FCR&DOC:emilywu@fahkco.com.hkTel:00852-23021977Fax:00852-2730217Transaction865320submittedVirginiaWong(T1281954U005) and Status is INCMP on 10-JUN-11 11.28.45.764386 PM -05:00"; //comment = comment.replaceAll("\\)", "\\\\)"); //comment = comment.replaceAll("\\(", "\\\\("); if(comment == null || comment.length() < 100) { System.out

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

半世苍凉 提交于 2019-12-02 20:14:05
问题 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.

Replacing characters got from javascript

天大地大妈咪最大 提交于 2019-12-02 15:56:11
问题 i'm making a program that extracts all the pictures from a flickr set. I found in the code a big String with every picture link, the problem is this: The links have the next format: https:\/\/c2.staticflickr.com\/4\/3925\/14562233192_3fe2b8fe1b_s.jpg but i'm unable of removing the '\' character, despite using the "\" escape sequence. My replacing code is the following, ret contains a lot of links separated by '\n': ret =ret.replaceAll("\\", ""); what in the world am i forgetting? My error

java String.replaceAll regex question

牧云@^-^@ 提交于 2019-12-02 13:28:56
I have a string that contains the following text String my_string = "hello world. it's cold out brrrrrr! br br"; I'd like to replace each isolated br with <br /> The issue is that I'd like to avoid converting the string to "hello world. it's cold out <br />rrrrr! <br /> <br />"; What I'd like to do is convert the string (using replaceAll) to "hello world. it's cold out brrrrrr! <br /> <br />"; I'm sure this is very simple, but my regex isn't correct. my_string.replaceAll("\\sbr\\s|\\sbr$", "<br />"); my regex is supposed to find 'whitespace' 'b' 'r' 'whitespace' OR 'whitespace' 'b' 'r' 'end of

Printing a replaced line in a new text file

纵然是瞬间 提交于 2019-12-02 12:57:28
I am trying to edit a matlabfile and replace some of the coding parts init in some specific lines. However, using the format below to make the changes it wont change the line context at all. (it will print the same old line). Any idea what am I doing wrong? 'replaceAll' is not suitable to replace some words with some others in the lines? Thanks in advance. try { PrintWriter out = new PrintWriter(new FileWriter(filenew, true)); Scanner scanner = new Scanner(file); while (scanner.hasNextLine()) { String line = scanner.nextLine(); if (line.contains("stream.Values(strmatch('Test',stream.Components

Replace all method throws PatternSyntaxException

隐身守侯 提交于 2019-12-02 11:49:30
look at the following code: String comment = "1)FCR pick up in Hong Kong2)Local charges will be paiy in Hong Kong & in HK$.3)Booking:virginiawong@fahkco.com.hk4)FCR&DOC:emilywu@fahkco.com.hkTel:00852-23021977Fax:00852-2730217Transaction865320submittedVirginiaWong(T1281954U005) and Status is INCMP on 10-JUN-11 11.28.45.764386 PM -05:00"; //comment = comment.replaceAll("\\)", "\\\\)"); //comment = comment.replaceAll("\\(", "\\\\("); if(comment == null || comment.length() < 100) { System.out.println(); } String[] strArray = comment.split(" "); for (int i = 0; i < strArray.length; i++) { if