• Post Reply Bookmark Topic Watch Topic
  • New Topic

jsp query regarding String concat add

 
sam dims
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all I have following query
<% String Name="abc.lmn.xyz.kom";
String temp1=Name.replace('.',' ');
String[] wordss = temp1.split (" ");
out.print(wordss[0]); %><br><%
out.print(wordss[1]); %><br><%
out.print(wordss[2]); %><br><%
%>

the above code in jsp page working properly and gives output like
abc
lmn
xyz
but i want all three in one String is there any method i can concat them in single String and display at once

Thanks.
 
Vinoth Thirunavukarasu
Ranch Hand
Posts: 164
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Just print the String temp1;
String temp1=Name.replace('.','');
 
sam dims
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vinoth thirunavukarasu:
Hello,
Just print the String temp1;
String temp1=Name.replace('.','');


hi vinoth,
I have tried that it gives output like
abc lmn xyz kom
this but i want output :
abclmnxyzkom in a single string

Thanks & Regards,
Sam
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35743
412
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sam,
Then you'll want the version that takes a String parameter. Since there is no "empty character" but there is an empty string, this will remove the dots rather than the spaces.



The trick is that replaceAll takes something called a regular expression rather than a real literal string. In regular expressions, a dot is a special character which means to match any character. The backslash makes the dot literally match a dot. And the other backslash makes Java happy.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't want line breaks between the characters why are you emitting a line-break tag?
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since JDK 1.5 which came out almost 5 years ago you can also just use Strings in String#replace().

That said, I would use JSTL's fn:replace for this rather than a lousy scriptlet, or at least move that logic to some Java class like a servlet or bean.
[ December 25, 2008: Message edited by: Bauke Scholtz ]
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35743
412
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bauke Scholtz:
Since JDK 1.5 which came out almost 5 years ago you can also just use Strings in String#replace().

Nice! As you can see we aren't all the edge of 2004 yet . I use Java 5 some (can't imagine writing code with contains, generics, etc). At the same time, I still tend to forget some of the APIs. And I've been doing a lot of 1.4 reg exp work lately which bumped it out of my head...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!