Java String Concatenation
I have a issue with my java code. i asked the same question yesterday. I got answer but sorry it was my fault. My question is not clear.
I have code looks like this:
for(i = 0; i < geo.getTargets().length ; i++ )
{
if(geo.getTargets(i).getTargetType().equalsIgnoreCase("ProximityTarget"))
{
final ProximityTarget prox = (ProximityTarget)geo.getTargets(i);
prox.getGeoPoint().getLatitudeInMicroDegrees();
prox.getGeoPoint().getLongitudeInMicroDegrees();
prox.getRadiusDistanceUnits();
}
}
The above three method will give me some values.
I want these values to be place in this format:
circle:long:lat:radius |开发者_如何学Go circle:long:lat:radius | .....
Can any one help me in fixing this code. I would like these value to be concatenated in a single string in order to insert it into my database field.
Try this:
StringBuilder sb = new StringBuilder();
for(i = 0; i < geo.getTargets().length ; i++ ){
if(geo.getTargets(i).getTargetType().equalsIgnoreCase("ProximityTarget")){
final ProximityTarget prox = (ProximityTarget)geo.getTargets(i);
float longitude = prox.getGeoPoint().getLatitudeInMicroDegrees());
float lat = prox.getGeoPoint().getLongitudeInMicroDegrees());
float radius = prox.getRadiusDistanceUnits();
if (sb.isEmpty()) {
sb.append("circle:" + longitude + ":" + lat + ":" + radius);
else {
sb.append(" | circle:" + longitude + ":" + lat + ":" + radius);
}
}
}
String result = sb.toString();
This is the basic way of doing what you asked for with String +
operator.
String result = "";
for(i = 0; i < geo.getTargets().length ; i++ ){
if(geo.getTargets(i).getTargetType().equalsIgnoreCase("ProximityTarget")){
final ProximityTarget prox = (ProximityTarget)geo.getTargets(i);
float longitude = prox.getGeoPoint().getLatitudeInMicroDegrees());
float lat = prox.getGeoPoint().getLongitudeInMicroDegrees());
float radius = prox.getRadiusDistanceUnits();
if (!result.isEmpty()) {
result += "|";
}
result += ("circle:" + longitude + ":" + lat + ":" + radius);
}
}
return result;
public String asString(ProximityTarget target) {
StringBuilder sb = new StringBuilder("circle:");
sb.append(target.getGeoPoint().getLatitudeInMicroDegrees()).append(":");
sb.append(target.getGeoPoint().getLongitudeInMicroDegrees()).append(":");
sb.append(target.getRadiusDistanceUnits());
return sb.toString();
}
public void someMethod() {
//...
StringBuilder sb = new StringBuilder();
for(i = 0; i < geo.getTargets().length ; i++ )
{
if(geo.getTargets(i).getTargetType().equalsIgnoreCase("ProximityTarget"))
{
final ProximityTarget prox = (ProximityTarget)geo.getTargets(i);
if (!sb.isEmpty())
sb.append("|");
sb.append(asString(prox));
}
}
String formattedString = sb.toString();
//...
}
The most basic way of doing Java string concatenation is to use the +
operator. Set up:
String value = string1 + string2 + string3;
There are other ways to do it, but this simple case should handle your needs. For further information, look into StringBuilder
.
Depends on the kind of environment you're on. If you don't need thread safety, use stringbuilder. The stringbuffer class provides the necessary synchronisation when needed.
moreover, if it is possible to estimate the size of the concatenated string, you could use StringBuffer(int capacity)/StringBuilder(int capacity) to avoid resizing
Additions to Thierry-Dimitri Roys response.
StringBuilder sb = new StringBuilder();
ProximityTarget prox;
float latitude;
float longitude;
float radius;
//Use for-each if you process all elements
for(Target target : geo.getTargets()){
//Use literal string first to avoid NullPointerException
if("ProximityTarget".equalsIgnoreCase(target.getTargetType())){
prox = (ProximityTarget)geo.getTargets(i);
latitude = prox.getGeoPoint().getLatitudeInMicroDegrees());
longitude = prox.getGeoPoint().getLongitudeInMicroDegrees());
radius = prox.getRadiusDistanceUnits();
//Checking sb every loop is not a good practice.
//Instead try removing first delimeter after for-loop
sb.append("|circle:" + longitude + ":" + latitude + ":" + radius);
}
}
//AFAIK StringBuilder has no isEmpty Method.
//So use length method
if (sb.length() > 0){
sb.deleteCharAt(0);
}
String result = sb.toString();
精彩评论