Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Escape % and other characters in String.format()

 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I have a method B using String.format( ) on some String arrays coming out from other method A.
Now String arr[] returned by A ,can be { "mov" ,"abc"} or {"%mov", "%abc" ,"adc"}.I have no control over it.

In my method B i do -
String rx=String.format("("+ StringUtils.join(arr,"|"))+")"; but it fails with UnknownformatException when it reads "%mov" kind.

How can i escape % like characters without making any changes to A and minimal changes in B.
I know % is used to escape format-specifiers as characters.But my method B is multiple purpose and also used when plain strings come from A .



 
Saloon Keeper
Posts: 14325
321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should pass them as format arguments.

String.format("(%s)", StringUtils.join(arr, "|"));
 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I figured out if i do
String rx ="(" + StringUtils.join(arr, "|" ) +")";
I get same result.
Are these same?
And
Is this correct to use above - performance wise and programming-style wise?Because In my opinion String.format also calls Formatter and makes it an expensive call.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lucky J Verma wrote:I figured out if i do
String rx ="(" + StringUtils.join(arr, "|" ) +")";
I get same result.


I was going to ask you "Why are you calling String.format() at all?". Because I don't see why you would want to call String.format() on that string.
 
When people don’t understand what you are doing they call you crazy. But this tiny ad just doesn't care:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic