Java String Concatenation

后端 未结 7 1712
無奈伤痛
無奈伤痛 2021-01-21 14:45

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:

相关标签:
7条回答
  • 2021-01-21 14:51

    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();
    
    0 讨论(0)
  • 2021-01-21 14:53

    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

    0 讨论(0)
  • 2021-01-21 14:54

    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;
    
    0 讨论(0)
  • 2021-01-21 15:00
    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();
    
        //...
    }
    
    0 讨论(0)
  • 2021-01-21 15:01

    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.

    0 讨论(0)
  • 2021-01-21 15:10

    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.

    0 讨论(0)
提交回复
热议问题