Tuesday 17 January 2012

Concatenating integers in java

Suppose we have following values:

int a=1; 
int b=0;
int c=2;
int d=2;
int e=1;


If we want to concatenate these we can use 2 approaches:

Approach 1 : Using usual mathematics
int result = a * 10000 + b * 1000 + c * 100 + d * 10 + e;
String s = Integer.toString(result);

Approach 2 : Appending using StringBuilder
StringBuffer sb = new StringBuffer();
sb.append(a).append(b).append(c)...

No comments:

Post a Comment