本文共 5337 字,大约阅读时间需要 17 分钟。
public class Demo04String01 { public static void main(String[] args) { 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(charArray); System.out.println("第三个字符串是:"+str3); String str4 = "Hello"; System.out.println(str4); }}
注意:直接写上双引号,就是字符串对象。
==是对字符串地址的比较,如果需要比较字符串的内容,可以使用两个方法:
public boolean equals (Object obj) : 参数可以是任何对象
public boolean equalsIgnoreCase (String str) :忽略大小写比较
public class Demo04String02 { public static void main(String[] args) { String str1 = "ABC"; String str2 = "ABC"; char [] charArray = { 'A','B','C'}; String str3 = new String(charArray); System.out.println(str1.equals(str2));//true System.out.println(str1.equals(str3));//true System.out.println(str3.equals("ABC"));//true System.out.println("ABC".equals(str1));//true }}
String中与获取相关的常用方法有:
public int length() :获取字符串当中含有的字符个数,拿到字符串长度。
public String concat(String str) :将当前字符串和和参数字符串拼接成为返回值新的字符串。
public char charAt (int index) :获取指定索引位置的单个字符。(索引从0开始)。
public int indexOf(String str) :查找参数字符串在本字符串当中首次出现的索引位置,如果没有返回 -1 值。
public class Demo04String03 { public static void main(String[] args) { //获取字符串长度 int length = "asdsafgwfaesdf".length(); System.out.println(length); //拼接字符串 String str1 = "Hello"; String str2 = "World"; String str3 = str1.concat(str2); System.out.println(str3); //获取指定索引位置的单个字符 char c = "Hello".charAt(3); System.out.println(c); //查找参数字符串在本来字符串中第一次出现的索引位置 String benlai = "HelloWorld"; int llo = benlai.indexOf("llo"); System.out.println(llo); }}
字符串的截取方法:
public String substring(int index):截取从参数位置一直到末尾,返回新字符串。
public String substring(int begin,int end):截取从begin到end结束中间的字符串。(左闭右开区间)
public char[] toCharArray (): 将当前字符串拆分成为字符数组作为返回值。
public byye[] getBytes(): 获得当前字符串底层的字节数组。
public String replace(CharSequence oldString,charSequence newString):将所有出现的老字符串替换为新字符串。
package demo04;public class Demo04String04 { public static void main(String[] args) { 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 str = null; String str1 = "how do you do"; String o = str1.replace("o", "*"); System.out.println(o); System.out.println("======================"); String lang1 = "wdnmd,我带你们打"; String wdnmd = lang1.replace("wdnmd", "*****"); System.out.println(wdnmd); }}
public String [] split(String regex) : 按照参数的规则,将字符串分为若干部分。
package demo04;public class Demo04String04 { public static void main(String[] args) { String str1 = "aaa,bbb,ccc"; String[] split = str1.split(","); for (int i = 0; i < split.length; i++) { System.out.println(split[i]); } }}
split方法的参数其实是一个正则表达式。 如果按照英文句点“.”进行切分,必须写 “\ \ .”
题目:定义一个方法,把数组{1,2,3}按照指定格式拼接成一个字符串。格式参照如下:[word1#word2#word3#]。
package demo04;public class Demo04String04 { public static void main(String[] args) { int [] array1 = { 1,2,3}; String array2 = fromAtoB(array1); System.out.println(array2); } public static String fromAtoB (int[] array){ String str = "["; for (int i = 0; i < array.length; i++) { if (i == array.length-1){ str += "word"+array[i]+"]"; } else str += "word"+array[i]+"#"; } return str; }}
题目:键盘输入一个字符串,并且统计其中各种字符出现的次数。
种类有:大写字母、小写字母、数字、其他。
package demo04;import java.util.Scanner;public class Demo04String04 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入一个字符串:"); String input = sc.next(); int daxie = 0; int xiaoxie = 0; int shuzi = 0; int qita = 0; char[] chars = input.toCharArray(); for (int i = 0; i < chars.length; i++) { char ch = chars[i]; if ('A' <= ch && ch <= 'Z') { daxie++; } else if ('a' <= ch && ch <= 'z') { xiaoxie++; } else if ('0' <= ch && ch <= '9') { shuzi++; } else { qita++; } } System.out.println("大写字母有:"+daxie+"个"); System.out.println("小写字母有:"+xiaoxie+"个"); System.out.println("数字有:"+shuzi+"个"); System.out.println("其他有:"+qita+"个"); }}
转载地址:http://jbjsi.baihongyu.com/