组团学

字符串-String

阅读 (737497)

1、String概述

1.1、什么是String类

String 类用于比较两个字符串,查找和抽取串中的字符或子串、字符串与其他类型之间相互转换等。String 类是一个常量对象,String 类对象的内容一旦被初始化就不能再被改变。

1.2、String构造方法

public String(): 创建一个字符串对象,其字符串值为空。

public String (String value):用字符串对象value创建一个新的字符串对象。

public String (char[] value):用字符数组value来创建字符串对象。

public String (char[]value,int offset,int count):从字符数组value中下标为offset的字符开始,创建有count个字符的字符串对象()

public String(byte ascII[]):用byte型字符串数组ascII,按缺省的字符编码方案创建字符串对象。

public String(byte ascII[],int offset int count)):从字节型数组ascII中下标为offset的字符开始,按缺省的字符编码方案创建count个字符的串对象。

public String(StringBuffer buffer):用缓冲字符串buffer创建一个字符串对象。

public class StringDemo01{ public static void main(String args[]){ String s1="hello"; String s2="world"; String s3="hello"; //按值比较 System.out.println(s1==s3); s1=new String ("hello"); s2=new String ("hello"); //按地址比较 System.out.println(s1==s2); //按值比较 System.out.println(s1.equals(s2)); char c[]={'s','u','n',' ','j','a','v','a'}; //将一个字符数组,转化为一个字符串 String s4=new String(c); //从下标为3的字符开始(包含下标为3的位置)取4个字符 String s5=new String(c,3,4); System.out.println(s4); System.out.println(s5); } }

运行结果:

image20200114150535474.png

2、常用方法

2.1、常用方法1

public char charAt(int index):返回字符串中第index个字符。

public int length():返回字符串的长度。

public int indexOf(String str):返回字符串中出现str 的第一个下标位置。

public int indexOf(String str,int fromIndex):返回字符串中从fromIndex开始出现str的第一个位置。

public boolean equalsIgnoreCase(String another):比较字符串是否与another一样。

public String replace(char oldChar,char newChar):字符串中用newchar替代oldChar字符。

public class StringDemo02{ public static void main(String args[]){ String s1="sun java"; String s2="sun Java"; //取出指定下标位置的字符 System.out.println(s1.charAt(1)); //取出字符串长度 System.out.println(s2.length()); //返回字符串中出现java的第一个下标位置 System.out.println(s1.indexOf("java")); System.out.println(s1.indexOf("Java")); //比较二个字符串是否相同,区分大小写 System.out.println(s1.equals(s2)); //比较二个字符串是否相同,不区分大小写 System.out.println(s1.equalsIgnoreCase(s2)); String s="我是程序员,我在学java"; //将字符串中的我,替换成你 String sr=s.replace('我','你'); System.out.println(sr); } }

运行结果:

image20200114151156713.png

2.2、常用方法2

public boolean startsWith(String prefix):判断字符串是否以prefix字符串开头。

public boolean endsWith(String suffix):判断字符串是否以suffix字符结尾。

public String toUpperCase():返回一个字符串为该字符串的大写形式。

public String toLowerCase():返回一个字符串为该字符串的小写形式。

public String substring(int beginIndex):返回字符串从beginIndex开始到结尾的子字符串。

public String substring(int beginIndex,int endIndex):返回字符串从beginIndex开始到endIndex结尾的子字符串。

public String trim():返回将该字符串去掉开头和结尾空格后的字符串。

public class StringDemo03 { public static void main(String args[]){ String s="Welcome to Java World"; String s1=" sun java "; //判断字符串是否以Welcome开始 System.out.println(s.startsWith("Welcome")); //判断字符串是否以World结束 System.out.println(s.endsWith("World")); //把字符串全部转换为小写 String sL=s.toLowerCase(); //把字符串全部转换为大写 String sU=s.toUpperCase(); System.out.println(sL); System.out.println(sU); //返回从下标11开始到结尾的字符串 String subS=s.substring(11); System.out.println(subS); //去掉字符串头、尾的空格 String sp=s1.trim(); System.out.println(sp); } }

运行结果:

image20200114152241666.png

2.3、常用方法3

public static String valueOf(…)可以将基本类型数据转换为字符串

例如:

public static String valueOf(double d)

public static String valueOf(int i)

public String []split(String regex):可以将一个字符串按照指定的分隔符分隔,返回分隔后的字符串数组。

public class StringDemo04 { public static void main(String args[]){ int j=1234567; //将int类型,转换成String类型 String sNumber=String.valueOf(j); System.out.println("j 是"+sNumber.length()+"位数。"); String s ="Mary,F,1976"; //以,号分隔,将字符串拆分为字符数组 String sPlit[] =s.split(","); for(int i=0;i<sPlit.length;i++){ System.out.println(sPlit[i]); } } }

运行结果:

image20200114152142364.png

2.4、常用方法4

concat(String str):把字符串str附加在当前字符串的末尾。

如:

String str="Hello"; String newStr=str.concat("World"); System.out.println(str); //打印Hello System.out.println(newStr); //打印HelloWorld

注意:以上concat()方法并不会改变字符串str本身的内容。

public class StringDemo05 { public static void main(String[] args) { // 打印 str1 String str1 = "self"; System.out.println(str1); // 打印 str2 连接了 str1 String str2 = str1.concat(" learning"); System.out.println(str2); // 打印 str3 连接了 str2 String str3 = str2.concat(" center"); System.out.println(str3); } }

运行结果:

image20200114152600219.png

需要 登录 才可以提问哦