开发者

to get the unique name by using starting letter of a string and concatenate with an integer

I need to get the unique name from a string and concatenate with an integer in java.So I want to take first letter of the string and increment that letter with an integer. Example: tenant name: "ani,raj,rob" and i need to get the schema name like a001,r001,r002 here r value will be incremented because of repetition.So I request you to help me find answer for this.I am getting the first letter from a string but I need to concatenate that with an integer.

String name = new String(tenantName); 
char sc=tenantName.charAt(0); 
String whereClause="tenant_id= select max(tenant_id) from tenant_connection_details 
          开发者_如何学JAVA                              tcd1 where schema_name like'"+sc+"%'"; 
tenantList= tenantImpl.getAllTenantsByWhereClause(whereClause); 


List<String> myNames = new ArrayList<String>();
myNames.add("ani");
myNames.add("raj");
myNames.add("rob");
myNames.add("jigar");

int counter=1;
for(String str:myNames){
System.out.println(str.charAt(0)+String.format("%04d", i));
counter++;
}


if i understand you correctly, it should be something like this (not safe or checked or anything!!):

    private List<String> list = new ArrayList<String>();

    private void strangeConcat(String name){
    int index = 1;

    while(this.list.contains(String.valueOf(name.charAt(0)) 
            + this.getThreeDigitIndex(index))){
        index++;
    }

    this.list.add(String.valueOf(name.charAt(0)) + this.getThreeDigitIndex(index));
}


Something like the following:

char sc=tenantName.charAt(0); // May use Character.toLowerCase() here, if only lowercase letters are allowed
String whereClause="tenant_id= select max(tenant_id) from tenant_connection_details 
                                    tcd1 where schema_name like'"+sc+"%'"; 
tenantList= tenantImpl.getAllTenantsByWhereClause(whereClause);
int number = tenentList.size() + 1;
String result = Character.toString(sc) + String.format("%04d", number);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜