首页 > 编程 > Java > 正文

java基础学习API之String类 六-1

2019-11-08 00:46:07
字体:
来源:转载
供稿:网友

类 String

String 类代表字符串。java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现。在 lang包下,所以不需要导包。API介绍:

一构造方法;

package wsj02;/** *  * @author Angus *  构造方法 *  String()  *  String(byte[] bytes)  *  String(byte[] bytes, int offset, int length)  *  String(char[] value)  *  String(char[] value, int offset, int count)  *  String(String original)  *   *  成员方法: *  	length() 长度 *   */public class StringDemo {	public static void main(String[] args) {		//方式一:String() 		String s1 = new String();		System.out.PRintln("s1:"+s1);//结果s1:     对象不是输出的地址值,证明重写了父类的方法。		System.out.println("s1.length:"+s1.length()); //s1.length:0   长度为零		  		//方式二 String(byte[] bytes) 		byte[] bys = {97,98,99,100,101};		String s2 = new String(bys);		System.out.println("s2:"+s2);//s2:abcde    编码表		System.out.println("s2.length:"+s2.length()); //s2.length:5		  		//方式三 String(byte[] bytes, int offset, int length) 		String s3 = new String(bys, 2, 3);		System.out.println("s3:"+s3);//s3:cde		System.out.println("s3.length:"+s3.length()); //s3.length:3				//方式三  String(String original) 		String s4 = new String("abcd");		System.out.println("s4:"+s4);//s4:abcd		System.out.println("s4.length:"+s4.length()); //	s4.length:4		//优化可以直接赋值		String s = "abcde";  	}}

疑问?

String s1 = new String();System.out.println("s1:"+s1);//结果s1:    和空     

对象new对象 输出为什么不是地址值呢?

输出不是地址值就是对string 的 toString 方法进行了重写,跟进源码可以发现:

   /**     * Initializes a newly created {@code String} object so that it represents     * an empty character sequence.  Note that use of this constructor is     * unnecessary since Strings are immutable.     */    public String() {        this.value = "".value;    }

构造API如下图:

疑问?

package wsj02;/** *  * @author Angus *	知识点1: 字符串一旦初始化就不可以被改变 *	知识点2; String s1 = new String("abc");和String s2 = "abc" 有区别吗? */public class StringDemo3 {	public static void main(String[] args) {		//知识点1: 字符串一旦初始化就不可以被改变		String s = "hello";		s += "world";		System.out.println(s); //helloworld		//为什么输出helloworld?		// 常量池值不可以被改变 但是引用s可以改变				//知识点2; String s1 = new String("abc");和String s2 = "abc" 有区别吗?		//new 会在堆内存中创建对象 然后再常量池中赋值  相当于两个对象		//s2 直接在常量池中找对象,有创建没有不创建。			}}

二String类的方法

部分方法操作:

package wsj02;/** * @author Angus *	booleane  equals(String str) //判断字符串的内容是否相同,区分大小写 *	booleane  equalsIgnoreCase(String str) //判断字符串的内容是否相同,不考虑大小写。 *	booleane  contains(String str)  //当且仅当此字符串包含指定的 char 值序列时,返回 true。 *	booleane  startsWith(String prefix)   //测试此字符串是否以指定的前缀开始。 *	booleane  endsWith(String suffix)  //测试此字符串是否以指定的后缀结束。 *	booleane  isEmpty() //当且仅当 length() 为 0 时返回 true。 */public class StringDemo4 {	public static void main(String[] args) {		String s = "HelloWorld";				//booleane  equals(String str) //判断字符串的内容是否相同,区分大小写		System.out.println(s.equals("HelloWorld"));		System.out.println(s.equals("helloworld"));		System.out.println("----------------------");				//booleane  equalsIgnoreCase(String str) //判断字符串的内容是否相同,不考虑大小写。		System.out.println(s.equalsIgnoreCase("HelloWorld"));		System.out.println(s.equalsIgnoreCase("helloworld"));		System.out.println("----------------------");				//booleane  contains(CharSequence s)  //当且仅当此字符串包含指定的 char 值序列时,返回 true。		System.out.println(s.contains("or"));		System.out.println(s.contains("ak"));		System.out.println("----------------------");				//booleane  startsWith(String prefix)   //测试此字符串是否以指定的前缀开始。		System.out.println(s.startsWith("H"));		System.out.println(s.startsWith("h"));		System.out.println("----------------------");				//booleane  endsWith(String suffix)  //测试此字符串是否以指定的后缀结束。		//......				//booleane  isEmpty() //当且仅当 length() 为 0 时返回 true。		System.out.println(s.isEmpty());		String s2 ="";		String s3 =null;		System.out.println(s2.isEmpty());		System.out.println(s3.isEmpty());		//Exception in thread "main" java.lang.NullPointerException		//at wsj02.StringDemo4.main(StringDemo4.java:44)		//空指针报错,判断是数据是否为空 null本身是空操作无意思。			}}

其它方法总结:

1.获取    1.1 获取字符串长度     int length();   1.2 根据位置获取字符     char charAt(int index);   1.3 根据字符获取在字符中的位置      int indexof(int ch) 返回的是ch在字符串中第一个出现的位置      int indexof(int ch,int FromIndex) 从fromIndex指定位置开始,获取ch在字符串中出现的位置      int indexof(String str); 返回的是str在字符串中第一个出现的位置      int indexof(String str,int FromIndex) 从fromIndex指定位置开始,获取str在字符串中出现的位置      反响索引一个字符出现的位置。      int lastindexof(int ch) 返回的是ch在字符串中第一个出现的位置      int lastindexof(int ch,int FromIndex) 从fromIndex指定位置开始,获取ch在字符串中出现的位置      int lastindexof(String str); 返回的是str在字符串中第一个出现的位置      int lastindexof(String str,int FromIndex) 从fromIndex指定位置开始,获取str在字符串中出现的位置    1.4获取字符串中的一部分字符串,也叫子串。        String subString(int beginindex,intendindex) ;      String subString(int beginindex) 2.判断   2.1两个字符串是否相同      equals(Object obj)      equalsIgnoreCase(String str)   2.2字符串中是否包含某个字符串      contains(String str)   2.3两个字符串是否以指定字符串开头或结尾      boolean Startswith(String);      boolean endswith(String);   2.4字符串是否为空      boolean isEmpty();  3.字符串转换      3.1将字符串变成字符串数组       String[] split(String regex);   3.2将字符串变成字符数组      char[] toCharArray();   3.1将字符串变成字节数组      byte[] getBytes();   3.4将字符串数组变成字符串      构造函数 String(char[])              String(char[],offset,count)将字符数组中的一部分转成字符串。       静态函数 static String copyValueof(char[])                static String copyValueof(char[],offset,count)将字符数组中的一部分转成字符串。   3.5将字符串的字母大小写转换       String toUppercase();大写       String toUppercase();小写   3.6将字符串的内容替换       String repalce(char oldch,char newch);       String repalce(String s1,String s2);   3.7将字符串两端空格去掉       String trim();   3.8将字符串进行连接       String concat(String);4.比较  compareTo();小返回负数 等返回0 大返回正数

package wsj02;import java.util.Scanner;/** * @author Angus */public class StringDemo4 {	// 键盘录入字符串 实现排序 大小写转换	public static void main(String[] args) {		Scanner sc = new Scanner(System.in);		System.out.println("请输入英文字母:");		String s = sc.nextLine();		char[] ch = s.toCharArray();		System.out.println(ch);		for (int i = 0; i < ch.length; i++) { // 实现大小写转换 第一种方法			if (ch[i] >= 'a' && ch[i] <= 'z') {				ch[i] -= 32;			} else if (ch[i] >= 'A' && ch[i] <= 'Z') {				ch[i] += 32;			}		}		System.out.println(ch);		// 第二种		StringBuilder st = new StringBuilder();		for (int x = 0; x < ch.length; x++) {			if (ch[x] >= 'a' && ch[x] <= 'z') {				String ss = String.valueOf(ch[x]);// Character.toString				st.append(ss.toUpperCase());			} else {				String ss = String.valueOf(ch[x]);				st.append(ss.toLowerCase());			}		}		String s1 = new String(st);		System.out.println(s1);	}}

最后附上JDK使用文档API 下载


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表