//
public class StringTest {
public static void main(String[] args) {
String str = "abcdefghijk";
String newstr = getStr(str);
System.out.println(newstr);
}
//
static String getStr(String s) {
String newStr = new String(); //get length of string
int temp = s.length();
if(temp % 2 == 0) {
for(int i = 1; i <= (temp - 1); i += 2) { //rebuild string
newStr += s.charAt(i);
}
}
else {
for(int i = 1; i <= (temp - 2); i += 2) { //rebuild string
newStr += s.charAt(i);
}
}
return newStr;
}
}
public class StringTest {
public static void main(String[] args) {
String str = "abcdefghijk";
String newstr = getStr(str);
System.out.println(newstr);
}
//
static String getStr(String s) {
String newStr = new String(); //get length of string
int temp = s.length();
if(temp % 2 == 0) {
for(int i = 1; i <= (temp - 1); i += 2) { //rebuild string
newStr += s.charAt(i);
}
}
else {
for(int i = 1; i <= (temp - 2); i += 2) { //rebuild string
newStr += s.charAt(i);
}
}
return newStr;
}
}
public static void main(String[] args) {
String str = "abcdefghijk";
String newstr = getStr(str);
System.out.println(newstr);
} static String getStr(String s) {
String newStr = new String(); // get length of string
int temp = s.length();
for (int i = 1; i < temp; i += 2) {
newStr += s.charAt(i);
} return newStr;
}
}
一个循环就搞定的啊
i < temp反正不会越界的
public static void main(String[] args) throws Exception {
String s = "asdfbdfasfdsa";
char[] chars = s.toCharArray();
StringBuffer sb = new StringBuffer();
for(int i = 0; i < chars.length; i += 2){
sb.append(chars[i]);
}
System.out.println(sb.toString());
}