String 我怎样才能使它更优雅,适用于任何字符串?
//但还是要明白这一点:String 我怎样才能使它更优雅,适用于任何字符串?,string,char,String,Char,//但还是要明白这一点: public class JavaIntern{ public static void main(String []args){ String str = "JavaIntern"; //hardcoded, but not the problem char[] s = str.toCharArray(); String result = new String (s,0,1); //this is whe
public class JavaIntern{
public static void main(String []args){
String str = "JavaIntern"; //hardcoded, but not the problem
char[] s = str.toCharArray();
String result = new String (s,0,1); //this is where the dilemma begins
System.out.println(result);
String result1 = new String (s,0,2);
System.out.println(result1);
String result2 = new String (s,0,3);
System.out.println(result2);
String result3 = new String (s,0,4);
System.out.println(result3);
String result4 = new String (s,0,5);
System.out.println(result4);
String result5 = new String (s,0,6);
System.out.println(result5);
String result6 = new String (s,0,7);
System.out.println(result6);
String result7 = new String (s,0,8);
System.out.println(result7);
String result8 = new String (s,0,9);
System.out.println(result8);
String result9 = new String (s,0,10);
System.out.println(result9); //and this is where it ends... how can I get rid of this?
}
}
我猜您希望改进代码,并且不依赖于字符串的长度 像这样的怎么样
J
Ja
Jav
Java
JavaI
JavaIn
JavaInt
JavaInte
JavaInter
JavaIntern
这还将打印:
public class JavaIntern{
public static void main(String []args){
String str = "JavaIntern"; //hardcoded, but not the problem
String substring = "";
for (char ch: str.toCharArray()) {
substring += ch;
System.out.println(substring);
}
}
}
循环一次获取字符串的一个字符,并在打印之前将其连接到子字符串。我猜您希望改进代码,并且不依赖于字符串的长度 像这样的怎么样
J
Ja
Jav
Java
JavaI
JavaIn
JavaInt
JavaInte
JavaInter
JavaIntern
这还将打印:
public class JavaIntern{
public static void main(String []args){
String str = "JavaIntern"; //hardcoded, but not the problem
String substring = "";
for (char ch: str.toCharArray()) {
substring += ch;
System.out.println(substring);
}
}
}
循环一次获取字符串的一个字符,并在打印之前将其连接到子字符串。我假设您希望每次能够多打印一个字母。 为此,我们使用一个
for
循环,这样做相当简单
public类MainClass{
公共静态void main(字符串[]args){
String str=“JavaIntern”;
对于(inti=1;i我假设您希望每次能够多打印一封信。
为此,我们使用一个for
循环,这样做相当简单
public类MainClass{
公共静态void main(字符串[]args){
String str=“JavaIntern”;
对于(int i=1;我不确定你的问题是什么。你到底想做什么,你期望什么,哪里出了问题?我不确定你的问题是什么。你到底想做什么,你期望什么,哪里出了问题?是的,就是那个!谢谢你!是的,就是那个!谢谢你!