0
 String a= request.getParameter("a");

I have following output

 out.println(a);// January 2019February 2019March 2019// prints output

I want the following output

       January 2019 February 2019 March 2019

i.e. adding space after the number 2019

I tried doing like this but didn't work out, any help much appreciated

    String a= request.getParameter("a")+"\t";// didn't work out
2
  • String a = request.getParameter("a")+"\t" just adds a tabulator at the end of the String. The problem is the formatting of the value returned by request.getParameter("a"). Can you change that? If not, you may want to split the String, after every number occurrence, maybe. Commented Jun 9, 2019 at 7:16
  • I am unable to change the output actually that's why I have to format it after getting output Commented Jun 9, 2019 at 7:18

1 Answer 1

4

You can find all the digit-letter boundaries and replace them with a space, using replaceAll with this regex:

(\d)([A-Z])

and this replacement:

$1 $2

The regex captures a digit into group 1, and captures a capital letter right after that digit into group 2. We replace the digit and the letter with whatever is in group 1, followed by a space, followed by whatever is in group 2.

You can use it like this:

String a = request.getParameter("a").replaceAll("(\\d)([A-Z])", "$1 $2");
Sign up to request clarification or add additional context in comments.

3 Comments

Hi thanks it worked out, just one doubt will it work in all browsers?
@Tom As far as know, JSP runs on the server, so it doesn't matter.
thanks a lot for help... upvoted and accepting ur ans

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.