Java向字符串添加字符 [英] Java add chars to a string
问题描述
我在一个java程序中有两个字符串,我想以某种方式混合形成两个新的字符串。为此,我必须从每个字符串中挑选一些组成字符,并添加它们以形成新的字符串。我有一个这样的代码(this.eka和this.toka是原始字符串):
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):
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);
我获取.charAt(x)零件的数字,到字符串?
I'm getting numbers for the .charAt(x) parts, so how do I convert the chars to string?
推荐答案
只需使用 substring()
$ c> charAt()
Just use always use substring()
instead of 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);
当位置isnt不是固定值而是变量时
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);
其中x,y,z是保存从哪里提取的位置的变量
where x,y,z are the variables holding the positions from where to extract
这篇关于Java向字符串添加字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!