Shortening code for string substitution I have this short function, it converts a human selected name like &quot;Accelerometer&quot; to &quot;Acc&quot; which a sensor can use:
private final String[] sensorNames = {&quot;Acc&quot;, &quot;Gyro&quot;, &quot;Magn&quot;, &quot;IMU9&quot;, &quot;Temp&quot;};

/**
 * Converts human representation of sensor names to movesense names. TODO: better code
 * @param sensorType Human representation of sensor name.
 * @return Short name of the sensor.
 */
private String findShortSensorName(String sensorType) {
    for (int i = 0; i &lt; sensorNames.length; i++) {
        if (sensorType.substring(0,1).equals(sensorNames[i].substring(0,1))) {
            return sensorNames[i];
        }
    }
    return &quot;&quot;;
}

Is there a way to make this more efficient or use less code? The return &quot;&quot;; is also bugging me. Thanks for your input!
It's hard to comment really well on this as your problem specification is more than a little vague.
Do you really want to do this based only on the first letter - is &quot;AbominableSnowman&quot; to be mapped to &quot;Acc&quot;?
Or do you know that the input sensor types are drawn from a specific set of values?
You could make your method (Java doesn't have functions) more succint, for example like this
private String findShortSensorName(String sensorType) {
  for (String sensorName : sensorNames) {
      if (sensorName.startsWith(sensorType.substring(0,1))) {
          return sensorName;
      }
  }
  return &quot;&quot;;
}

However, the idiomatic approach for lookup in Java is usually to use a Map, and I'd have thought that if the value isn't found, throwing an Exception may be more appropriate than returning an empty string, though that will depend on your application. As your method doesn't look tied to a particular Java object, it can probably be &quot;static&quot;, as well.
My version isn't shorter (it's considerably longer), but it's idiomatic (in my view) and easier to safely extend.
class BadSensorTypeException extends Exception {
// Thrown when a sensor type isn't found in the lookup table

  public BadSensorTypeException(String detailMessage) {
    super(detailMessage);
  }
 }

/**
 * lookup table mapping sensor types to short movesense names
 */
private static final Map&lt;String, String&gt; sensorNames = new HashMap&lt;&gt;();
static {
// static block to initialise the lookup table
  sensorNames.put(&quot;Accelerometr&quot;, &quot;Acc&quot;);
  sensorNames.put(&quot;Gyroscope&quot;, &quot;Gyro&quot;);
  // etc
}

/**
 * Converts human representation of sensor names to movesense names.
 * @param sensorType Human representation of sensor name.
 * @return Short name of the sensor.
 * @throws BadSensorTypeException if the sensor type isn't found
 */
private static String findShortSensorName(String sensorType) throws BadSensorTypeException {
  String shortName = sensorNames.get(sensorType);
  if (shortName != null) {
    return shortName;
  }
  throw new BadSensorTypeException(String.format(&quot;%s is not a valid sensor type&quot;, sensorType));
}