Java String
java.lang.String类代表字符串。 程序当中的“”(双引号),都是String类的对象。就算没有new,也照样是。
字符串的特点
1.字符串的内容永不可变。 2.正是因为字符串不可改变,所以字符串是可以共享的。 3.字符串效果上相当于char[]字符数组,但是底层原理是byte[]字节数组。
创建方式
public String(); public String(char[] array); public String(byte[] array); String str = “hello”;//直接创建
一些常用方法
返回类型 | 方法 | 描述 |
---|
boolean | equals(Object obj) | 进行内容比较 | boolean | equalIgnoreCase(String str) | 忽略大小写,进行内容比较 | int | length() | 获取字符串长度 | String | concat(String str) | 拼接字符串 | char | charAt(int index) | 获取指定索引位置的单个字符,从0开始 | int | indexOf(String str) | 查找参数字符串当中首次出现的索引位置,若无,返回-1. | String | substring(int index) | 截取从参数位置一直到字符串末尾,返回新字符串 | String | substring(int begin,int end) | 截取从begin开始,到end结束,中间的字符串。左开右闭 | char[] | toCharArray() | 将当前字符串拆分成为字符数组作为返回值 | byte[] | getBytes() | 获得当前字符串底层的字节数组 | String | replace(CharSequence oldString,CharSequennce newString | 将所有出现的老字符串替换成新的字符串,返回替换之后的结果新字符串 | String[] | split(String regex) | 按照参数的规则,将字符串切分成为若干部分。 |
一些常用方法代码
String str1 = new String();
System.out.println("第一个字符串:" + str1);
char[] charArray = {'A', 'B', 'C'};
String str2 = new String(charArray);
System.out.println("第二个字符串:" + str2);
byte[] byteArray = {97, 98, 99};
String str3 = new String(byteArray);
System.out.println("第三个字符串:" + str3);
String str4 = "wenwen";
System.out.println("第四个字符串:" + str4);
String str1 = "Hello";
String str2 = "Hello";
char[] charArray = {'H', 'e', 'l', 'l', 'o'};
String str3 = new String(charArray);
System.out.println(str1 == str2);
System.out.println(str1 == str3);
System.out.println(str1.equals(str2));
System.out.println(str1.equals(str3));
System.out.println("Hello".equals(str1));
String str4 = "hello";
System.out.println(str1.equals(str4));
System.out.println("============");
String strA = "Java";
String strB = "java";
System.out.println(strA.equals(strB));
System.out.println(strA.equalsIgnoreCase(strB));
int length = "abcedjfjjafk".length();
System.out.println("字符串的长度是:" + length);
String str1 = "i love ";
String str2 = "wenwen";
System.out.println(str1.concat(str2));
char ch = "ilovewenwen".charAt(1);
System.out.println("在1号索引位置的字符:" + ch);
System.out.println("w第一次出现的位置索引:" + "ilovewenwen".indexOf("w"));
String str3 = "ilovewenwen";
String str4 = str3.substring(5);
System.out.println(str4);
String str5 = str3.substring(5, 8);
System.out.println(str5);
char[] chars = "Hello".toCharArray();
System.out.println(chars[0]);
System.out.println(chars.length);
System.out.println("===========");
byte[] bytes = "abc".getBytes();
for (int i = 0; i < bytes.length; i++) {
System.out.println(bytes[i]);
}
System.out.println("==========");
String str6 = "how do you do ?";
String str7 = str6.replace("o", "*");
System.out.println(str7);
System.out.println("===========");
String lang1 = "fuck you";
String lang2 = lang1.replace("fuck", "****");
System.out.println(lang2);
System.out.println("=========");
String str8 = "aaa,bbb,ccc,dd";
String[] arry1 = str8.split(",");
for (int j = 0; j < arry1.length; j++) {
System.out.println(arry1[j]);
}
System.out.println("=========");
String str9 = "aaa bbb ccc";
String[] arry2 = str9.split(" ");
for (int k = 0; k < arry2.length; k++) {
System.out.println(arry2[k]);
}
System.out.println("========");
String str10 = "xxx.zzz.yyy";
String[] arry3 = str10.split("\\.");
System.out.println(arry3.length);
for (int m = 0; m < arry3.length; m++) {
System.out.println(arry3[m]);
}
System.out.println("========");
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入一个字符串");
String inputStr = scan.next();
int countUpper = 0;
int countLower = 0;
int countNumber = 0;
int countOther = 0;
char[] chArray = inputStr.toCharArray();
for (int j = 0; j < chArray.length; j++) {
char ch = chArray[j];
if ('A' <= ch && ch <= 'Z'){
countUpper++;
}else if('a'<=ch && ch <= 'z'){
countLower++;
}else if('0'<= ch && ch <= '9'){
countNumber++;
}else {
countOther++;
}
}
System.out.println("大写字母个数:" + countUpper);
System.out.println("小写字母个数:" + countLower);
System.out.println("数字个数:" + countNumber);
System.out.println("其他字符个数:" + countOther);
}
|