在特定位置的字符串中插入字符

dav*_*cks 139 java string casting

我的价格是int6位数.我希望将它显示为String带有小数点(.)的结尾处的2位数int.我想使用a float但建议String用于更好的显示输出(而不是1234.51234.50).因此,我需要一个函数,它将采用intas参数并返回正确格式化String的结尾,小数点为2位数.

说:

int j= 123456 
Integer.toString(j); 

//processing...

//output : 1234.56
Run Code Online (Sandbox Code Playgroud)

blo*_*p3r 196

正如在注释中提到的,StringBuilder可能是一个更快的实现,然后使用StringBuffer.正如Java文档中提到的:

此类提供与StringBuffer兼容的API,但不保证同步.此类设计用作StringBuffer的替代品,用于单个线程使用字符串缓冲区的位置(通常情况下).在可能的情况下,建议首先使用此类优先于StringBuffer,因为在大多数实现中它会更快.

用法:

String str = Integer.toString(j);
str = new StringBuilder(str).insert(str.length()-2, ".").toString();
Run Code Online (Sandbox Code Playgroud)

或者,如果您需要同步,请使用具有类似用法的StringBuffer:

String str = Integer.toString(j);
str = new StringBuffer(str).insert(str.length()-2, ".").toString();
Run Code Online (Sandbox Code Playgroud)


Mik*_*sen 179

int j = 123456;
String x = Integer.toString(j);
x = x.substring(0, 4) + "." + x.substring(4, x.length());
Run Code Online (Sandbox Code Playgroud)

  • 没有循环,这是一个简单的连接案例,编译器应该使用字符串构建器对其进行优化,为了便于阅读,我更喜欢使用+运算符,在这种情况下不需要显式地使用StringBuilder.使用"StringBuilder"解决方案,因为它更快,不尊重优化规则.可读性代码.在分析后进行优化,并且仅在需要时进行优化.http://en.wikipedia.org/wiki/Program_optimization#Quotes (29认同)
  • 在第二个子字符串调用中不需要x.length(). (16认同)
  • 忘记StringBuilder.使用这样的格式,[String.format](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#format%28java.lang.String,%20java.lang .Object ...%29)是最好的选择. (7认同)
  • 字符串是不可变的.虽然这很有效,但使用像StringBuilder这样的东西在道德上是正确的,更不用说会使你的代码更快. (5认同)

Ita*_*man 17

int yourInteger = 123450;
String s = String.format("%6.2f", yourInteger / 100.0);
System.out.println(s);
Run Code Online (Sandbox Code Playgroud)


ghe*_*kin 8

使用 ApacheCommons3 StringUtils,你也可以做

int j = 123456;
String s = Integer.toString(j);
int pos = s.length()-2;

s = StringUtils.overlay(s,".", pos, pos);
Run Code Online (Sandbox Code Playgroud)

它基本上是子字符串连接,但如果您不介意使用库,或者已经依赖于 StringUtils,则它会更短


the*_*e64 6

对于 Kotlin 伙计们;)来自已接受的答案(@MikeThomsen\'s)

\n
fun String.insert(insertAt: Int, string: String): String {\n    return this.substring(0, insertAt) + string + this.substring(insertAt, this.length)\n}\n
Run Code Online (Sandbox Code Playgroud)\n

测试\xe2\x9c\x85

\n
"ThisTest".insert(insertAt = 4, string = "Is").should.equal("ThisIsTest")\n
Run Code Online (Sandbox Code Playgroud)\n


rmu*_*ler 5

在大多数用例中,使用StringBuilder(如已回答的)是一个很好的方法来做到这一点。但是,如果性能很重要,这可能是一个不错的选择。

/**
 * Insert the 'insert' String at the index 'position' into the 'target' String.
 * 
 * ````
 * insertAt("AC", 0, "") -> "AC"
 * insertAt("AC", 1, "xxx") -> "AxxxC"
 * insertAt("AB", 2, "C") -> "ABC
 * ````
 */
public static String insertAt(final String target, final int position, final String insert) {
    final int targetLen = target.length();
    if (position < 0 || position > targetLen) {
        throw new IllegalArgumentException("position=" + position);
    }
    if (insert.isEmpty()) {
        return target;
    }
    if (position == 0) {
        return insert.concat(target);
    } else if (position == targetLen) {
        return target.concat(insert);
    }
    final int insertLen = insert.length();
    final char[] buffer = new char[targetLen + insertLen];
    target.getChars(0, position, buffer, 0);
    insert.getChars(0, insertLen, buffer, position);
    target.getChars(position, targetLen, buffer, position + insertLen);
    return new String(buffer);
}
Run Code Online (Sandbox Code Playgroud)