- 字符串的常量池
- 字符串相关的比较方法
public class Demo01String {
public static void main(String[] args) {
String s1 = "Java";
String s2 ="java";
System.out.println(s1.equalsIgnoreCase(s2));//true 忽略大小写
System.out.println(s1.equals(s2));//false 区分大小写
}
}
- 字符串的获取
public class Demo01String {
public static void main(String[] args) {
int length = "ssdfdffdfdfdfdfdfdfd".length();
System.out.println("字符串长度"+length);
String s1 = "hello";
String s2 = "world";
String s3 = s1.concat(s2);
System.out.println(s3); //helloworld
char ch = s1.charAt(1);
System.out.println("s1的1号索引位置的字符是"+ch); //e
System.out.println(s1.indexOf("aaa")); //-1 返回aaa第一次索引的位置,因为没有所以返回-1
System.out.println(s1.indexOf("el")); //1
System.out.println("abcabc".indexOf("ca"));//2
}
}
- 字符串的分割
package com.qml.study;
public class Demo02String {
public static void main(String[] args) {
String string = "aaa,bbb,vvvv";
String[] arry = string.split(",");
for (int i = 0; i < arry.length; i++) {
System.out.println(arry[i]); //aaa 换行bbb 换行vvv
}
System.out.println("------------------");
String string2 = "aaa.bbb.ccc";
String[] str2 = string2.split("\\."); //split用.分割记得带\\.
for (int i = 0; i < str2.length; i++) {
System.out.println(str2[i]);
}
}
}
/*把数组{1,2,3} 打印出字符串 [a1#a2#a3]
- */
package com.qml.study;
/*把数组{1,2,3} 打印出字符串 [a1#a2#a3]
* */
public class Demo03String {
public static void main(String[] args) {
int[] array = {1,2,3};
System.out.println( func(array)); //[a1#a2#a3]
}
public static String func(int[] array){
String string1 = "[";
for (int i = 0; i < array.length; i++) {
if (i == array.length-1){
string1 += "a"+array[i]+"]";
}
else {
string1 += "a"+array[i]+"#";
}
}
return string1;
}
}
/**
- 键盘输入一个字符串,并统计各类字符出现的次数。
- 种类:大写字母,小写字母,数字,其他
*/
package com.qml.study;
import java.util.Scanner;
/**
* 键盘输入一个字符串,并统计各个字符出现的次数。
* 种类:大写字母,小写字母,数字,其他
*/
public class Demo04String {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入:");
String string = scanner.next();
int contUpper = 0 ;
int contLower = 0;
int contNum = 0;
int other = 0;
char[] chars = string.toCharArray();
for (int i = 0; i < chars.length; i++) {
char ch = chars[i];
if ('A' <= ch && ch <= 'Z')
{contUpper++;
}else if ('a'<= ch && ch <='z'){
contLower++;
}
else if('0' <= ch && ch <= '9'){
contNum++;
}
else {other++;}
}
System.out.println(contUpper);
System.out.println(contLower);
System.out.println(contNum);
System.out.println(other);
}
}