Ravi Jain
Ravi Jain

Reputation: 1482

String formatting for inserting values into Db

I have a string as -> "10,25,12,25,30".

What I want is a string in which numbers are enclosed within round brackets.

So, after formatting, the string should look like this -> (10),(25),(12),(25),(30).

Is there any any short way of performing this kind of formatting.

I want this format of string to insert a large numbers of values in DB.

I am using Java.

Any ideas, please?

Thanks.

Upvotes: 0

Views: 50

Answers (2)

Rohit Bansal
Rohit Bansal

Reputation: 1199

This is yet another method to do the same...

    String result = "";
    String s = "10,25,12,25,30";

    // Split string using String Tokenization
    String tokens[] = s.split(",");
    for(String token : tokens) {
        result += "("+token+"),";
    }

    // To remove the last comma 
    result = result.substring(0, result.length()-1);

Upvotes: 0

Ferdau
Ferdau

Reputation: 1558

If you want a fast/easy way:

String s = "10,25,12,25,30";
s = s.replace(",", "),(";
s = "("+s+")";

Upvotes: 4

Related Questions