Java向字符串添加字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1654546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:42:39  来源:igfitidea点击:

Java add chars to a string

javastringchar

提问by rize

I have two strings in a java program, which I want to mix in a certain way to form two new strings. To do this I have to pick up some constituent chars from each string and add them to form the new strings. I have a code like this(this.eka and this.toka are the original strings):

我在 java 程序中有两个字符串,我想以某种方式将它们混合以形成两个新字符串。为此,我必须从每个字符串中提取一些组成字符并将它们添加到新字符串中。我有这样的代码(this.eka 和 this.toka 是原始字符串):

String muutettu1 = new String();
String muutettu2 = new String();
muutettu1 += this.toka.charAt(0) + this.toka.charAt(1) + this.eka.substring(2);
muutettu2 += this.eka.charAt(0) + this.eka.charAt(1) + this.toka.substring(2);
System.out.println(muutettu1 + " " + muutettu2);

I'm getting numbers for the .charAt(x) parts, so how do I convert the chars to string?

我正在获取 .charAt(x) 部分的数字,那么如何将字符转换为字符串?

采纳答案by jitter

Just use always use substring()instead of charAt()

只需使用始终使用substring()而不是charAt()

muutettu1 += toka.substring(0,1) + toka.substring(1,2) + eka.substring(2);
muutettu2 += eka.substring(0,1) + eka.substring(1,2) + toka.substring(2);

e.g. when the position arent isn't fixed values but variables do

例如,当位置不是固定值但变量是

muutettu1 += toka.substring(x,x+1) + toka.substring(y,y+1) + eka.substring(z);
muutettu2 += eka.substring(x,x+1) + eka.substring(y,y+1) + toka.substring(z);

where x,y,z are the variables holding the positions from where to extract

其中 x,y,z 是保存要提取的位置的变量

回答by Thomas Jung

The obvious conversion method is Character.toString.

显而易见的转换方法是Character.toString

A better solution is:

更好的解决方案是:

String muutettu1 = toka.substring(0,2) + eka.substring(2);
String muutettu2 = eka.substring(0,2) + toka.substring(2);

You should create a method for this operation as it is redundant.

您应该为此操作创建一个方法,因为它是多余的。

The string object instatiantiation new String()is unnecessary. When you append something to an empty string the result will be the appended content.

字符串对象实例化new String()是不必要的。当您将某些内容附加到空字符串时,结果将是附加的内容。

回答by alphazero

StringBuilder builder = new StringBuilder();
builder
   .append(this.toka.charAt(0))
   .append(this.toka.charAt(1))
   .append(this.toka.charAt(2))
   .append(' ')  
   .append(this.eka.charAt(0))
   .append(this.eka.charAt(1))
   .append(this.eka.charAt(2));
System.out.println (builder.toString());

回答by Chàrìthà SG

This thing can adding a chars to the end of a string

这个东西可以在字符串的末尾添加一个字符

StringBuilder strBind = new StringBuilder("Abcd");
strBind.append('E');
System.out.println("string = " + str);
//Output => AbcdE
str.append('f');
//Output => AbcdEf

回答by Xiaogang

You can also convert an integer into a String representation in two ways: 1) String.valueOf(a) with a denoting an integer 2) Integer.toString(a)

您还可以通过两种方式将整数转换为字符串表示形式:1) String.valueOf(a) 用 a 表示整数 2) Integer.toString(a)