Replace all

S

soup_or_power

Hi All

Please tell me how nested statements are handled in java.
Here is an example:

String st="abcdefghijk";

st=st.replaceAll("a", "x").replaceAll("b", "y").replaceAll("c", "z");

The expected result is xyzdefghijk.

2nd way:

st=st.replaceAll("a", "x");
st=st.replaceAll("b", "y");
st=st.replaceAll("c", "z");

I have 2 questions:

a) the first one doesn't work for complicated expressions: example
replaceAll("&abc", "")
given as arguments to replaceAll

b)is there any advantage to the first given that un-interned strings
are allocated new memory locations?

Thanks for your help.
 
S

Stefan Ram

Please tell me how nested statements are handled in java.

Statements can be nested usind a compound statement, which
also is being called a »block«. A block is a statement itself
and is being executed by executing each nested statement in
the given sequence.

http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.2

There are also other statments with »nested« substatements,
like the if statement.
Here is an example:
String st="abcdefghijk";
st=st.replaceAll("a", "x").replaceAll("b", "y").replaceAll("c", "z");

This is not an example of nested statements, but a sequence of
two statements.
The expected result is xyzdefghijk.

Such a sequence does not have a »result«.

(Methods might have a result.)
 
D

Daniel Pitts

Hi All

Please tell me how nested statements are handled in java.
Here is an example:

String st="abcdefghijk";

st=st.replaceAll("a", "x").replaceAll("b", "y").replaceAll("c", "z");

The expected result is xyzdefghijk.

2nd way:

st=st.replaceAll("a", "x");
st=st.replaceAll("b", "y");
st=st.replaceAll("c", "z");
Both of these are equivalent. in output.
I have 2 questions:

a) the first one doesn't work for complicated expressions: example
replaceAll("&abc", "")
given as arguments to replaceAll
What do you mean it doesn't work? That should replace all strings that
are "&abc" with the blank string.
b)is there any advantage to the first given that un-interned strings
are allocated new memory locations?
Both forms are equivalent in that regard. The number of interned strings
vs not interned strings is the same for both cases. String.replaceAll
returns a String. Whether you store a reference to it or call a method
on it, the new String is still created.
Thanks for your help.
No problem.
 
S

soup_or_power

Both of these are equivalent.  in output.



What do you mean it doesn't work? That should replace all strings that
are "&abc" with the blank string.


Both forms are equivalent in that regard. The number of interned strings
vs not interned strings is the same for both cases.  String.replaceAll
returns a String. Whether you store a reference to it or call a method0000
on it, the new String is still created.


No problem.

Sorry for confusing between nested statements and sequence statements.
It seems in both of the examples given above, the number of String
objects created is the same. Is the sequnce preferred way to write
code? Is it style vs. substance kind of thingy?

My other question is, the sequence statement worked in a small java
program I wrote before making my first post. (Due diligence done:).
However, when I used it inside a servlet that is called using Ajax, it
didn't work. This is the syntax of the sequence statement I had:

st.replaceAll("a", "x")
..replaceAll("b", "y")
..replaceAll("c", "z")

Please note the carriage returns/new lines. I had to use carriage
returns/new lines because the statement is too big for the Eclipse IDE
to display in one line.

Thanks
 
D

Daniel Pitts

Sorry for confusing between nested statements and sequence statements.
It seems in both of the examples given above, the number of String
objects created is the same. Is the sequnce preferred way to write
code? Is it style vs. substance kind of thingy?
Style. What ever you deem to be more readable for the particular
situation is what should be done.
My other question is, the sequence statement worked in a small java
program I wrote before making my first post. (Due diligence done:).
However, when I used it inside a servlet that is called using Ajax, it
didn't work. This is the syntax of the sequence statement I had:

st.replaceAll("a", "x")
..replaceAll("b", "y")
..replaceAll("c", "z")

Please note the carriage returns/new lines. I had to use carriage
returns/new lines because the statement is too big for the Eclipse IDE
to display in one line.

Thanks
If it didn't work in the servlet, then quite possibly you are calling it
incorrectly. Note that Strings are immutable, so replaceAll must return
a modified copy, not modify the original.
 
L

Lew

What *precisely* do you mean by "it didn't work"?

IOW, what did you expect to happen, and what happened instead?

Two dots in a row as you show here would have failed to compile. Did
it?

Java is not line-oriented, so the newlines make absolutely no
difference.

Daniel said:
If it didn't work in the servlet, then quite possibly you are calling it
incorrectly. Note that Strings are immutable, so replaceAll must return
a modified copy, not modify the original.

Daniel raises the question of what you did with the result of the
expression you showed us.

Assuming it even compiled (which would mean that you misquoted it on
Usenet).
 
L

Lew

Two dots in a row as you show here would have failed to compile.  Did
it?

Daniel raises the question of what you did with the result of the
expression you showed us.

Assuming it even compiled (which would mean that you misquoted it on
Usenet).

Oops, I see you weren't the one to misquote it.
 
T

Tom Anderson

It seems in both of the examples given above, the number of String
objects created is the same. Is the sequnce preferred way to write code?
Is it style vs. substance kind of thingy?

Yes. They're exactly the same in terms of what they do; it's just a matter
of which way you find more readable. It's a bit like the choice in natural
language (english, at least) of writing a long sentence with several
clauses separated by semicolons, or a sequence of shorter sentences.
My other question is, the sequence statement worked in a small java
program I wrote before making my first post. (Due diligence done:).
However, when I used it inside a servlet that is called using Ajax, it
didn't work. This is the syntax of the sequence statement I had:

st.replaceAll("a", "x")
.replaceAll("b", "y")
.replaceAll("c", "z")

Please note the carriage returns/new lines. I had to use carriage
returns/new lines because the statement is too big for the Eclipse IDE
to display in one line.

It should work identically in both.

tom

--
Imagine a city where graffiti wasn't illegal, a city where everybody
could draw wherever they liked. Where every street was awash with a
million colours and little phrases. Where standing at a bus stop was never
boring. A city that felt like a living breathing thing which belonged to
everybody, not just the estate agents and barons of big business. Imagine
a city like that and stop leaning against the wall - it's wet. -- Banksy
 
R

Roedy Green

st=st.replaceAll("b", "y");
st=st.replaceAll("c", "z");

These functions are badly named. See
http://mindprod.com/jgloss/string.html

You get nailed by unexpected use of regexes and unexpected multiple
replacements.

functions are evaluated left to right.

See http://mindprod.com/jgloss/precedence.html
--
Roedy Green Canadian Mind Products
http://mindprod.com
PM Steven Harper is fixated on the costs of implementing Kyoto, estimated as high as 1% of GDP.
However, he refuses to consider the costs of not implementing Kyoto which the
famous economist Nicholas Stern estimated at 5 to 20% of GDP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,588
Members
45,100
Latest member
MelodeeFaj
Top