0

Hi all I a have the following code in my java application

DROP TABLE IF EXISTS "countries";
CREATE TABLE "countries" (
  "CNT_ISO2" varchar(2) NOT NULL,
  "CNT_CODEN" int(11) NOT NULL,
  "CNT_NAME" varchar(100) NOT NULL,
  "CNT_NAME_SHORT" varchar(50) NOT NULL,
  "CNT_CONTINENT" int(11) DEFAULT NULL,
  PRIMARY KEY ("CNT_ISO2")
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

using java I want to replace all varchar with another character say text

eg:- Both varchar(2) and varchar(50) should be replaced with text

Thanks......

2 Answers 2

2

Try the following code:

stmt.replaceAll("varchar(\\(\\d+\\))?", "text");
Sign up to request clarification or add additional context in comments.

4 Comments

Thank u Dmytro thats working fine ...can u please explain that..?
Rakhesh, this is replacement by Regular Expression. All the \ signs are needed to escape the special meaning of the parentheses.
@DmytroChyzhykov It's kind of lame to just copy my answer, don't you feel that way? It's what I call "karma vulture" behavior. The proper way was to edit my answer and just add two characters into it. I understand, with your rep still in the low three-digit range, but as you move on, please do mind the rules of conduct.
@Marko Topolnik. Sorry for that. Nothing personal. I've voted your comment in place.
1

I think your solution can be as easy as

stmt.replaceAll("varchar(\\(\\d+\\))?", "text")

6 Comments

No, .replace takes a char, not a string. I think you mean stmt.replaceAll("varchar([0-9]*)", "text");
@PaulTomblin Please, do bother to actually check the Javadoc. But you are right in pointing out that the parenthesized numbers need to be removed.
@Marko Topolnik. There is a few mistakes in your code. Just double the first and the last / symbols.
@MarkoTopolnik, unlike you I did check the Javadoc. Why don't you follow the link you posted, and you'll see that the signature for .replace is either String replace(char oldChar, char newChar) or String replace(CharSequence target, CharSequence replacement). Neither takes two Strings as an argument.
You are correct. I missed that String is now a subclass of CharSequence. My apologies.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.