0

I have a Java project in which I am working in our "query module". It is responsible for building queries from string we call "snippets". To do that, I am using things like this

myQueryOuter{
               %s
}

myInnerQuery{
      QUERY TEXT
}

Using String.Format will work, but now one of my queries accepts values, which are coded as "%s", so an exception is thrown when I call the String.format method.

Is there anyway to escape the %s? That way I would create the whole query and then apply a String.Format to replace the %s for the values.

Or is there any nicer way to do something like this?

Thanks!

Edit: Let me clarify:

I have the following

myQueryOuter{
                   %s
}
myInnerQuery{
          The next blank space should be filled by a parameter: %s
    }

I want to make

String output = String.Format(myOuterQuery, myInnerQuery) //this throws an exception

So I have this output

myQueryOuter{ myInnerQuery{ The next blank space should be filled by a parameter: %s } }

So I can call

String realOutput = String.format(output, "ThisIsMyParameter")

So I have

myQueryOuter{ myInnerQuery{ The next blank space should be filled by a parameter: this is my output } }

The exception is:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.util.MissingFormatArgumentException: Format specifier '%s'

Because I have %s twice, but I want my output to HAVE the %s

JSBach
  • 4,679
  • 8
  • 51
  • 98

1 Answers1

3

Before you format you need to replace % with %%

Then to format %%s with your parameter.. This will do the work

Hope that helps

Aviad
  • 1,539
  • 1
  • 9
  • 24