Macsupport
Macsupport

Reputation: 5444

Replace carriage returns and line feeds in out.println?

I am a novice coder and I am using the following code to outprint a set of Image keywords and input a "|" between them.

<% Set allKeywords = new HashSet();
for (AlbumObject ao : currentObjects) {
XmpManager mgr = ao.getXmpManager();
if (mgr != null) {
allKeywords.addAll(mgr.getKeywordSet());    
  }
}
//get the Iterator
Iterator itr = allKeywords.iterator();
while(itr.hasNext()){
  String str = itr.next();
out.println(str +"|");   
} %>

I want the output to be like this:

red|blue|green|yellow

but it prints out:

red|
blue|
green|
yellow

which breaks my code. I've tried this:

str.replaceAll("\n", "");  
str.replaceAll("\r", ""); 

and

str.replaceAll("(?:\\n|\\r)", ""); 

No luck. I'd really appreciate some help!

Upvotes: 0

Views: 424

Answers (1)

Bill the Lizard
Bill the Lizard

Reputation: 406105

Just use out.print instead. That way you don't have to contend with the unwanted newlines.

Upvotes: 3

Related Questions