java 字符串练习:charAt()
package com.test001;
/**
* charAt () 从字符串中获取找到字符
*/
public class CharAt {
public static void main(String[] args) {
String s1 = "大家好,我是小飞,今天我们开始学习新的知识点,它名字是charAt()。";
String s2 = ", ,。! ?# % ¥ $ * ";
int count = 0;
for (int i = 0; i < s1.length(); i++) {
char c = s1.charAt(i); // 把字符串中的每个字符都拿出来查看是否存在于s2 中;
// System.out.print(c);
if (s2.indexOf(c) >= 0){ // 如有存在 count + 1 :表示有一句话
count += 1;
}
}
System.out.println("句子的数量为: " + count);
String str = " xiamianduicharatzhegefangfajinxingcheshi";
char cha = str.charAt(5);
System.out.println(cha); // 输出第五个字符
/**
* 把一个数组转化为字符串
*/
int [] arr = {23, 34, 45, 56, 67, 78, 89};
String result = "";
// System.out.println(result.length());
for (int i = 0; i < arr.length; i++) {
if (result.length() > 0)
result += ",";
result = result + arr[i];
}
System.out.println(result);
/**
* 现在写一个方法,根据这个字符串生成一个int[]
*/
String strs = "13,33,22,45,89";
String[] split = strs.split(",");
int[] ints = new int[split.length];
for (int i = 0; i < split.length; i++) {
ints [i] = Integer.valueOf(split[i]); // 这一步已经转化完成: int [] ints = {13,33,22,45,89};
}
for (int i = 0; i < ints.length; i++) {
System.out.print(ints[i]);
}
}
}