java---字符串操作(连接,获取信息,比较,替换)

来源:互联网 发布:qsv转换mp4软件 编辑:程序博客网 时间:2024/06/05 00:52

1、字符串连接: + 、concat() 

例一:

public class Test {public static void main(String[] args) {String str1 = "Hello " + "world!";System.out.println("使用+连接字符串:" + str1);String str2 = "Hello ".concat("world!");System.out.println("使用concat()方法连接字符串:" + str2);}}

运行结果为:

使用+连接字符串:Hello world!
使用concat()方法连接字符串:Hello world!


例二:

public class StringConcatenation {public static void main(String[] args) {System.out.println("1 + 2 = " + 1 + 2);System.out.println("1 + 2 = " + (1 + 2));System.out.println(6 + 7 + 'A');System.out.println(6 + 7 + "A");System.out.println(6 + "A" + 7);}}
运行结果为:

1 + 2 = 12
1 + 2 = 3
78
13A
6A7


2、获取字符串信息:length()、indexOf()、lastIndexOf()、charAt() 

public class Test {public static void main(String[] args) {String message = "So say time sffjver!";// 定义字符串// length是获得字符串的长度信息System.out.println(message + "的长度是:" + message.length());// indexOf是获得目标字符或者目标字符串在元字符串的首次出现的位置System.out.println("s首次出现的索引值:" + message.indexOf('o'));System.out.println("s首次出现的索引值:" + message.indexOf("ay"));// lastIndexOf是获得目标字符或者目标字符串在元字符串的末次出现的位置System.out.println("s末次出现的索引值:" + message.lastIndexOf('s'));System.out.println("s末次出现的索引值:" + message.lastIndexOf("ve"));System.out.println(message + "的奇数索引字符为:");for (int i = 0; i < message.length(); i++) {if (i % 2 == 1)System.out.print(message.charAt(i) + " ");} // charAt是获得字符创中每个字符的char值System.out.println();// intern是获得字符串对象的规范化表示形式System.out.println(message.intern());// codePointCount(begin,end)是获得制定文本范围中Unicode代码点数System.out.println(message.codePointCount(2, 8));}}
运行结果为:

So say time sffjver!的长度是:20
s首次出现的索引值:1
s首次出现的索引值:4
s末次出现的索引值:12
s末次出现的索引值:16
So say time sffjver!的奇数索引字符为:
o s y t m   f j e ! 
So say time sffjver!
6


3、字符串比较:equals()、equalsIgnoreCase()、compareTo()、 == 、
                          startsWith()、endsWith() 

public class StringEquals {public static void main(String[] args) {String message1 = "mrsoft"; // 定义字符串String message2 = "mrsoft "; // 定义字符串String message3 = "MrSoft"; // 定义字符串// equals精确比较System.out.println(message1 + " equals " + message2 + ": "+ message1.equals(message2));// equalsIsIgnoreCase忽略大小写比较System.out.println(message1 + " equalsIgnoreCase " + message3 + ": "+ message1.equalsIgnoreCase(message3));System.out.println();// compareTo是按字典顺序比较两个字符串,小于为负,等于为0,大于为正System.out.println(message1 + " compareTo " + message2 + ": "+ message1.compareTo(message2));System.out.println(message1 + " compareTo " + message3 + ": "+ message1.compareTo(message3));// compareToIgnoreCase是忽略大小写,比较的方法与compareTo一样System.out.println(message1 + " compareToIgnoreCase " + message3 + ": "+ message1.compareToIgnoreCase(message3));System.out.println();String s1 = "abc";String s2 = "abc"; // s1与s2指向同一个空间String s3 = new String("abc"); // s3是一个新的空间String s4 = new String("abc"); // s4是一个新的空间String s5 = s4; // s5与s4指向同一个空间System.out.println("s1==s2:" + (s1 == s2));System.out.println("s1==s3:" + (s1 == s3));System.out.println("s3==s4:" + (s3 == s4));System.out.println("s4==s5:" + (s4 == s5));System.out.println("s3==s5:" + (s3 == s5));}}
运行结果为:

mrsoft equals mrsoft : false
mrsoft equalsIgnoreCase MrSoft: true



mrsoft compareTo mrsoft : -1
mrsoft compareTo MrSoft: 32
mrsoft compareToIgnoreCase MrSoft: 0


s1==s2:true
s1==s3:false
s3==s4:false
s4==s5:true
s3==s5:false


public class StringSEDemo {public static void main(String[] args) {String message = "So say we all!";// 定义字符串// startsWith(字符串)判断是否以括号中的字符串作为前缀,注意括号中内容呢不能为字符boolean startsWith = message.startsWith("S");System.out.println(message + "以So作为前缀:" + startsWith);// endsWith(字符串)判断是否以括号中的字符串作为后缀,注意括号中内容呢不能为字符boolean endsWith = message.endsWith("!");System.out.println(message + "以!作为后缀:" + endsWith);}}

运行结果为:

So say we all!以So作为前缀:true
So say we all!以!作为后缀:true


4、字符串替换: replace()、replaceAll、replaceFirst() 

public class StringReplace {public static void main(String[] args) {String message = "So say we all!";// 定义字符串System.out.println("替换前字符串:" + message);// replace使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串,用后者替换前者String replace = message.replace(" ", "\n");System.out.println("替换后字符串:" + replace);System.out.println();String str = "aaabcd";System.out.println("替换前字符串:" + str);String str2 = str.replace("aa", "b");System.out.println("替换后字符串:" + str2);System.out.println();// replaceAll使用给定的replacement替换此字符串所有匹配给定的正则表达式的子字符串,用后者替换前者replace = message.replaceAll(" ", "\n");System.out.println("替换后字符串:" + replace);System.out.println();// replaceFirst使用给定的replacement替换此字符串所有匹配给定的正则表达式的第一个字符串,用后者替换前者replace = message.replaceFirst(" ", "\n");System.out.println("替换后字符串:" + replace);}}

运行结果为:

替换前字符串:So say we all!
替换后字符串:So
say
we
all!


替换前字符串:aaabcd
替换后字符串:babcd


替换后字符串:So
say
we
all!


替换后字符串:So
say we all!