字符运算
字符和整数之间的联系可通过查阅 ASCII 码表。两者之间是可以相互转化的。
’A'~'Z' 是 65~90,'a' - 'z' 是 97 - 122,0 - 9 是 48 - 57。
1 2 3 4 5 6 7 8
| public class demo1 { public static void main(String[] args) { int a='A'; System.out.println((int)a); int b=68; System.out.println((char)b); } }
|
字符串运算
在 Java 中字符串可以通过 + 进行拼接。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package string; public class demo1 { public static void main(String[] args) { String s1="123456"; String s2="7894556"; String s3=s1+s2; System.out.println("s1:"+s1); System.out.println("s2:"+s2); System.out.println("s3:"+s3); int sec=15006; int hour=sec/3600; int min=sec%3600/60; sec=sec%3600%60; System.out.println(hour+"小时"+min+"分钟"+sec+"秒"); } }
|
常用的字符串操作
在 Java 中提供一些常用的字符串操作函数。
length ():返回长度
split (String regex):分割字符串
indexOf (char c)、indexOf (String str)、lastIndexOf (char c)、lastIndexOf (String str):查找,找不到返回-1
equals ():判断两个字符串是否相等,注意不能直接用==
compareTo ():判断两个字符串的字典序大小,负数表示小于,0 表示相等,正数表示大于
startsWith ():判断是否以某个前缀开头
endsWith ():判断是否以某个后缀结尾
trim ():去掉首尾的空白字符
toLowerCase ():全部用小写字符
toUpperCase ():全部用大写字符
replace (char oldChar, char newChar):替换字符
replace (String oldRegex, String newRegex):替换字符串
substring (int beginIndex, int endIndex):返回[beginIndex, endIndex) 中的子串
toCharArray ():将字符串转化成字符数组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| public class demo1 { public static void main(String[] args) { String str1="Hello World"; System.out.println("字符串"+str1+"的长度是:"+str1.length()); System.out.println(); String csv = "apple,banana,orange,grape"; String[] fruits = csv.split(","); for (String fruit : fruits) { System.out.println(fruit); } System.out.println(); System.out.println("'o' 第一次出现位置: " + str1.indexOf('o')); System.out.println("'o' 最后一次出现位置: " + str1.lastIndexOf('o')); System.out.println("\"World\" 位置: " + str1.indexOf("World")); System.out.println("\"Java\" 位置: " + str1.indexOf("Java")); String str2 = "Hello World"; String str3 = new String("Hello World"); System.out.println("str1 == str2: " + (str1 == str2)); System.out.println("str1 == str3: " + (str1 == str3)); System.out.println("str1.equals(str3): " + str1.equals(str3)); System.out.println(); String a = "apple"; String b = "banana"; String c = "apple"; System.out.println("apple和banana比较:"+a.compareTo(b)); System.out.println("banana和apple比较:"+b.compareTo(a)); System.out.println("apple和apple比较:"+a.compareTo(c)); System.out.println(); String filename = "document.pdf"; System.out.println("这个文件开头是doc吗?"+filename.startsWith("doc")); System.out.println("这个文件是pdf格式吗?"+filename.endsWith(".pdf")); System.out.println(); String str4=" HELLO WORLD "; System.out.println("去除首尾空格前:"+str4); System.out.println("去除空格后:"+str4.trim()); System.out.println(); String str5="HELLO World"; System.out.println(str5+"全部转为大写为:"+str5.toUpperCase()); System.out.println(str5+"全部转为小写为:"+str5.toLowerCase()); System.out.println(); String text = "I love cats. Cats are cute."; System.out.println("原文本"+text); System.out.println("替换字符"+text.replace('I','i')); System.out.println("替换字符串"+text.replace("cats","dogs")); System.out.println("替换全部(不区分大小写): " + text.replaceAll("(?i)cats", "dogs")); System.out.println(); System.out.println("substring(6): " + text.substring(6)); System.out.println("substring(6, 10): " + text.substring(6, 10)); char[] charArray = text.toCharArray(); for (char ch : charArray) { System.out.print(ch + " "); } System.out.println(); } }
|
运行结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| 字符串Hello World的长度是:11
apple banana orange grape
'o' 第一次出现位置: 4 'o' 最后一次出现位置: 7 "World" 位置: 6 "Java" 位置: -1
str1 == str2: true str1 == str3: false str1.equals(str3): true
apple和banana比较:-1 banana和apple比较:1 apple和apple比较:0
这个文件开头是doc吗?true 这个文件是pdf格式吗?true
去除首尾空格前: HELLO WORLD 去除空格后:HELLO WORLD
HELLO World全部转为大写为:HELLO WORLD HELLO World全部转为小写为:hello world
原文本I love cats. Cats are cute. 替换字符i love cats. Cats are cute. 替换字符串I love dogs. Cats are cute. 替换全部(不区分大小写): I love dogs. dogs are cute.
substring(6): cats. Cats are cute. substring(6, 10): cat I l o v e c a t s . C a t s a r e c u t e .
|
字符串的输入输出
sc.next () 在遇到空格、回车等空白字符时停止输入。
sc.nextLine () 在遇到空格时不会停止输入。遇到回车才会停止。
StringBuilder、StringBuffer
String拼接字符串后原字符串还存在于内存中,浪费内存。
String 不能被修改,如果打算修改字符串,可以使用 StringBuilder 和 StringBuffer。
StringBuffer 线程安全,速度较慢;StringBuilder 线程不安全,速度较快。
1 2 3 4 5 6 7 8 9 10 11 12
| public class demo1 { public static void main(String[] args) { StringBuilder sb=new StringBuilder("Hello"); sb.append("World"); System.out.println(sb); String s=sb.toString(); System.out.println(s); sb.reverse(); System.out.println(sb); } }
|