Tatkal
Tatkal

Reputation: 568

How to replace String value with regex

MOrris Example

Morris.Bar({
  element: 'bar-example',
  data: [
    { month: "2006", amount: 100},
    { month: "2007", amount: 75}
  ],
  xkey: "month",
  ykeys: ["amount"],
  labels: ["amount"]
});

amount in ykeys and lables should be same as it is mentioned in Morris Example. Otherwise graph would not display because of error in amount format.

At moment my amount value is in this way

String amount = "[amount]";

amount="[amount]"

and i want value in this way

["amount"]

What would be easiest and preferred way to replace these values?

Upvotes: 1

Views: 76

Answers (2)

Mark W
Mark W

Reputation: 5964

You don't need to use regex.

  amount = amount.replace("[", "[\"").replace("]","\"]");

Edited. OP cleared up what was needed. The above code is replacing [ with [" and ] with "].

The " is escaped within java with \

Upvotes: 1

Azodious
Azodious

Reputation: 13872

Why regex?

If you can achieve your task with simple calls to APIs which don't require regex, stick to them.

amount.subString(1,amount.length()-1);

Upvotes: 3

Related Questions