public class Test {
public static void main(String args[]) {
String str = "helloworld";
char[] data = str.toCharArray();// 将字符串转为数组
for (int x = 0; x < data.length; x++) {
System.out.print(data[x] + " ");
data[x] -= 32;
System.out.print(data[x] + " ");
}
System.out.println(new String(data));
}
}
返回字符串最后一个单词的长度
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
if(in.hasNext()) {
String[] a = in.nextLine().split(" ");
System.out.println(a[a.length-1].length());
}else {
System.out.println(0);
}
in.close();
}
字符串大小写字母反转
static StringBuffer sb = new StringBuffer();
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
String str = in.nextLine();
System.out.println(exChange(str));
in.close();
}
public static String exChange(String str) {
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (Character.isUpperCase(c)) {
sb.append(Character.toLowerCase(c));
} else {
sb.append(Character.toUpperCase(c));
}
}
return sb.toString();
}
字符串反转
public static String reverse(String str) {
StringBuffer sb = new StringBuffer();
for (int i = str.length()-1; i>=0 ; i--) {
sb.append(str.charAt(i));
}
return sb.toString();
}
Java输入流
String str;
StringBuffer sb = new StringBuffer();
Scanner sc = new Scanner();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
do {
str = br.readLine();
sb.append(str);
} while (!str.equals("end"));
if (sc.hasNextLine()) {
sb.append(sc.nextLine());
}
System.out.println(sb.toString());