-2

I receive from internet-service html code:

String html = Service.getHtml();

In html i have for example:

<div style="text"></div>

i would like use in replace 'style="text"' to for example 'new="aaa"':

String new = html.replace("style=\"test\"", "new=\"aaa\"");

but this not working. It probably did not find the text with backslash.

user2864740
  • 60,010
  • 15
  • 145
  • 220
peksak
  • 59
  • 5
  • 1
    [Obligatory link](http://stackoverflow.com/a/1732454/2071828). Although not Regex, this is still a bad idea. Use an HTML parser like [JSoup](http://jsoup.org/). – Boris the Spider Aug 23 '14 at 17:20
  • 1
    I would have solved this immediately if you said anything regarding what was not working besides "this is not working." – djechlin Aug 23 '14 at 17:22
  • What *does* "not working" mean? Is that the *exact* code? Does it result in a *compiler error*? If not, what happens to make it "not work"? – user2864740 Aug 23 '14 at 17:29
  • 1
    Assuming that the OP copied the code, I believe he made the a spelling mistake: `html.replace("style=\"test\"", "new=\"aaa\"");` --> `html.replace("style=\"text\"", "new=\"aaa\"");` – kevin Aug 23 '14 at 17:33

1 Answers1

0

A problem is that new is a keyword in Java - you can't use it as a variable name! - and the posted code will not compile.

user2864740
  • 60,010
  • 15
  • 145
  • 220
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • @user2864740 yes it does. what are you talking about? – djechlin Aug 23 '14 at 17:22
  • @djechlin Assuming that the question is "why doesn't this compile?", perhaps. But I am not an Oracle. – user2864740 Aug 23 '14 at 17:23
  • @djechlin that's fine, if user2864740 thinks this answer is not useful I'm okay with his downvote :) – Nir Alfasi Aug 23 '14 at 17:24
  • @user2864740 poor you, this guy figured out that was exactly the problem, posted an answer, and now you're downvoting because you aren't the OP and don't actually see what the problem is. What you just said describes why it's a terrible *question*. I think these two downvotes are absurd but I'm not upvoting because I rather strongly feel it's a disservice to all parties involved to answer such awful questions. – djechlin Aug 23 '14 at 17:24
  • @user2864740 s/^/The problem is that / should fix this answer for you. – djechlin Aug 23 '14 at 17:25
  • @djechlin your suggestion sounds reasonable, wording issue is now fixed. – Nir Alfasi Aug 23 '14 at 17:26
  • @user2864740 btw, if can compile `String new = html.replace("style=\"test\"", "new=\"aaa\"");` in Java - I'm taking my hat off ;) – Nir Alfasi Aug 23 '14 at 17:28
  • @djechlin Terrible questions (as in questions which do not explain, or worse, exhibit behavior other than that which is explained) should not be honored with answers; rather, clarification should be sought in comments. – user2864740 Aug 23 '14 at 17:28
  • @user2864740 I know it's debatable: http://meta.stackexchange.com/questions/16621/how-to-handle-bad-questions-with-good-answers – Nir Alfasi Aug 23 '14 at 17:30