rucha
rucha

Reputation: 135

How to separate each declared string in another string by comma in spring mvc?

I am using Spring MVC. I want to separate each declared string in another string by using comma and without using an array. And I need to pass that comma separated string in another Java file. here is my code: below is the model having three different setters and getters

public String pk1=getInstitutionId();
public String pk2=getInstitutionName();
public String pk3=getIsoCountryCode();

And Now I want to split this by using comma like this:

private String MultipleprimaryKeyStr= pk1 + "," + pk2 + "," + pk3;

But this pk1, pk2 and pk3 are taking as separate string. But MultipleprimaryKeyStr should take it as variable.

Upvotes: 1

Views: 313

Answers (3)

Hridesh
Hridesh

Reputation: 71

By doing concatenation of multiple string into one string and those string is separated by comma. Ex. String MultiplePrimaryKeyStr="This, is, separated, string";

just pass this string to new java file. And split in that file by calling split().

String[] sepratedString=MultiplePrimaryKeyStr.split(","); process that string array inside loop.

Upvotes: 0

Rajnikant Patel
Rajnikant Patel

Reputation: 571

You have to do like

String MultipleprimaryKeyStr= params[0] + "," + params[1] + "," + params[2];

One more thing, I would like to tell you that you should not go in static but you should run while or for loop there like this:

String MultiplePrimaryKeyStr = "";
    for(int i = 0; i < params.length; i++)
    {
        MultiplePrimaryKeyStr = ((i!=0) ? "," : "") + params[i];
    }

Upvotes: 1

Amit
Amit

Reputation: 962

By doing pk1 + "," + pk2 + "," + pk3 you are creating a new String because the operator + is used for concatenation operation in Strings. So now you get this new String and you can split this String using split(",") as:

private String MultipleprimaryKeyStr= pk1 + "," + pk2 + "," + pk3;
String[] newStr= MultipleprimaryKeyStr.split(",");

Upvotes: 0

Related Questions