gpt4 book ai didi

java - 如何从一个字符串复制到Java中的另一个字符串?

转载 作者:行者123 更新时间:2023-12-03 20:25:02 25 4
gpt4 key购买 nike

我有两个字符串 str1 和 str2。我正在尝试使用 charAt 将一些字母从一个字符串复制到另一个字符串。我知道我可以使用字符串复制,但我想要一些字符而不是全部。

如何在 Java 中将 subString 从一个字符串复制到另一个字符串?

public class MyServerSide {

public static void main(String[] args) {
String str1 = "Hello World!;
String str2;
for (int 1=0; i < str1.length(); i++){
if (i>=3){
str2.charAt(i) = str1.charAt(i);//Here is the problem. It gives me an error
//Saying that the left argument must be a
//variable

}//End of if statement
}//End of for loop
}//End of main method
}//End of class

最佳答案

如果您只需要一些字符,请使用 String.substring(...)

编辑:

要将现有字符串与另一个字符串中的某些字符组合,您可以使用:

String anotherString = anotherString + originalString.substring(...);

要使用另一个字符串中的某些字符创建一个新字符串,您可以使用:

String aNewString = originalString.substring(...);

关于java - 如何从一个字符串复制到Java中的另一个字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16127786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com