【java基础】——String类和基本数据类型包装类

来源:互联网 发布:java把两个数组合并 编辑:程序博客网 时间:2024/06/04 18:46

一、String类

1、String类概述

①String类是字符串类型对象。Java程序中的所有字符串如“abc”都作为此类的实例实现。
②String类的特点
字符串常量,一旦创建之后不能更改。
String str = “abc”   等同于  char data[] ={'a','b','c'};  String str = new String(data);

2、String类的常见功能

①获取
  • length():返回此字符串的长度。
  • charAt(int index) :返回指定索引处的char 值。
  • hashCode() :返回此字符串的哈希码。
  • indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
  • indexOf(int ch, int fromIndex):返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。
  • indexOf(String str):返回指定子字符串在此字符串中第一次出现处的索引。
  • indexOf(String str, int fromIndex):返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。
  • lastIndexOf(int ch):返回指定字符在此字符串中最后一次出现处的索引。
String类获取功能代码演示:
package cn.itcast_04;/* * String类的获取功能 * int length():获取字符串的长度。 * char charAt(int index):获取指定索引位置的字符 * int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。 * 为什么这里是int类型,而不是char类型? * 原因是:'a'和97其实都可以代表'a' * int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。 * int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。 * int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。 * String substring(int start):从指定位置开始截取字符串,默认到末尾。 * String substring(int start,int end):从指定位置开始到指定位置结束截取字符串。 */public class StringDemo {public static void main(String[] args) {// 定义一个字符串对象String s = "helloworld";// int length():获取字符串的长度。System.out.println("s.length:" + s.length());System.out.println("----------------------");// char charAt(int index):获取指定索引位置的字符System.out.println("charAt:" + s.charAt(7));System.out.println("----------------------");// int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。System.out.println("indexOf:" + s.indexOf('l'));System.out.println("----------------------");// int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。System.out.println("indexOf:" + s.indexOf("owo"));System.out.println("----------------------");// int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。System.out.println("indexOf:" + s.indexOf('l', 4));System.out.println("indexOf:" + s.indexOf('k', 4)); // -1System.out.println("indexOf:" + s.indexOf('l', 40)); // -1System.out.println("----------------------");// 自己练习:int indexOf(String str,int// fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。// String substring(int start):从指定位置开始截取字符串,默认到末尾。包含start这个索引System.out.println("substring:" + s.substring(5));System.out.println("substring:" + s.substring(0));System.out.println("----------------------");// String substring(int start,int// end):从指定位置开始到指定位置结束截取字符串。包括start索引但是不包end索引System.out.println("substring:" + s.substring(3, 8));System.out.println("substring:" + s.substring(0, s.length()));}}
②转换
  • toCharArray():将此字符串转换为一个新的字符数组。
  • getBytes():将字符串转成字节数组。
  • toUpperCase() :将字符串的字符转成大写。
  • concat(String str):将字符串进行连接
String类转换功能代码演示:
package cn.itcast_05;/* * String的转换功能: * byte[] getBytes():把字符串转换为字节数组。 * char[] toCharArray():把字符串转换为字符数组。 * static String valueOf(char[] chs):把字符数组转成字符串。 * static String valueOf(int i):把int类型的数据转成字符串。 * 注意:String类的valueOf方法可以把任意类型的数据转成字符串。 * String toLowerCase():把字符串转成小写。 * String toUpperCase():把字符串转成大写。 * String concat(String str):把字符串拼接。 */public class StringDemo {public static void main(String[] args) {// 定义一个字符串对象String s = "JavaSE";// byte[] getBytes():把字符串转换为字节数组。byte[] bys = s.getBytes();for (int x = 0; x < bys.length; x++) {System.out.println(bys[x]);}System.out.println("----------------");// char[] toCharArray():把字符串转换为字符数组。char[] chs = s.toCharArray();for (int x = 0; x < chs.length; x++) {System.out.println(chs[x]);}System.out.println("----------------");// static String valueOf(char[] chs):把字符数组转成字符串。String ss = String.valueOf(chs);System.out.println(ss);System.out.println("----------------");// static String valueOf(int i):把int类型的数据转成字符串。int i = 100;String sss = String.valueOf(i);System.out.println(sss);System.out.println("----------------");// String toLowerCase():把字符串转成小写。System.out.println("toLowerCase:" + s.toLowerCase());System.out.println("s:" + s);// System.out.println("----------------");// String toUpperCase():把字符串转成大写。System.out.println("toUpperCase:" + s.toUpperCase());System.out.println("----------------");// String concat(String str):把字符串拼接。String s1 = "hello";String s2 = "world";String s3 = s1 + s2;String s4 = s1.concat(s2);System.out.println("s3:"+s3);System.out.println("s4:"+s4);}}
③判断
  • equals(Object anObject):比较两个字符串的内容是否相同。注:String类中的equals复写了Object中的equals,建立了String类自己的判断字:比较的是内容
  • equalsIgnoreCase(String anotherString):将此String 与另一个 String 比较,不考虑大小写。
  • contains(CharSequence s):字符串是否包含指定字符串。
String判断功能代码演示:
package cn.itcast_03;/* * String类的判断功能: * boolean equals(Object obj):比较字符串的内容是否相同,区分大小写 * boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写 * boolean contains(String str):判断大字符串中是否包含小字符串 * boolean startsWith(String str):判断字符串是否以某个指定的字符串开头 * boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾 * boolean isEmpty():判断字符串是否为空。 *  * 注意: * 字符串内容为空和字符串对象为空。 * String s = ""; * String s = null; */public class StringDemo {public static void main(String[] args) {// 创建字符串对象String s1 = "helloworld";String s2 = "helloworld";String s3 = "HelloWorld";// boolean equals(Object obj):比较字符串的内容是否相同,区分大小写System.out.println("equals:" + s1.equals(s2));System.out.println("equals:" + s1.equals(s3));System.out.println("-----------------------");// boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写System.out.println("equals:" + s1.equalsIgnoreCase(s2));System.out.println("equals:" + s1.equalsIgnoreCase(s3));System.out.println("-----------------------");// boolean contains(String str):判断大字符串中是否包含小字符串System.out.println("contains:" + s1.contains("hello"));System.out.println("contains:" + s1.contains("hw"));System.out.println("-----------------------");// boolean startsWith(String str):判断字符串是否以某个指定的字符串开头System.out.println("startsWith:" + s1.startsWith("h"));System.out.println("startsWith:" + s1.startsWith("hello"));System.out.println("startsWith:" + s1.startsWith("world"));System.out.println("-----------------------");// 练习:boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾这个自己玩// boolean isEmpty():判断字符串是否为空。System.out.println("isEmpty:" + s1.isEmpty());String s4 = "";String s5 = null;System.out.println("isEmpty:" + s4.isEmpty());// NullPointerException// s5对象都不存在,所以不能调用方法,空指针异常System.out.println("isEmpty:" + s5.isEmpty());}}
④替换
  • replace(char oldChar, char newChar) :将字符串中的内容进行替换
⑤切割
  • split(String regex):根据给定正则表达式的匹配拆分此字符串。
  • split(String regex, int limit):根据匹配给定的正则表达式来拆分此字符串。
⑥子串,获取子串的一部分
  • substring(int beginIndex):返回一个新的字符串,它是此字符串的一个子字符串。
  • substring(int beginIndex, int endIndex):返回一个新字符串,它是此字符串的一个子字符串。注意:包含头,不包含尾!
⑦去除空格,比较
  • trim():将字符串两端的空格去掉。
  • compareTo(String anotherString):按自然顺序比较两个字符串。
  • compareToIgnoreCase(String str):忽略大小写,按自然顺序比较两个字符串。
功能代码演示:
package cn.itcast_06;/* * String类的其他功能: *  * 替换功能: * String replace(char old,char new) * String replace(String old,String new) * * 去除字符串两空格 * String trim() *  * 按字典顺序比较两个字符串   * int compareTo(String str) * int compareToIgnoreCase(String str) */public class StringDemo {public static void main(String[] args) {// 替换功能String s1 = "helloworld";String s2 = s1.replace('l', 'k');String s3 = s1.replace("owo", "ak47");System.out.println("s1:" + s1);System.out.println("s2:" + s2);System.out.println("s3:" + s3);System.out.println("---------------");// 去除字符串两空格String s4 = " hello world  ";String s5 = s4.trim();System.out.println("s4:" + s4 + "---");System.out.println("s5:" + s5 + "---");// 按字典顺序比较两个字符串String s6 = "hello";String s7 = "hello";String s8 = "abc";String s9 = "xyz";System.out.println(s6.compareTo(s7));// 0System.out.println(s6.compareTo(s8));// 7System.out.println(s6.compareTo(s9));// -16}}
String小练习:统计字符串中子串的出现次数:
package cn.itcast_07;/* * 统计大串中小串出现的次数 * 举例: * 在字符串"woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun" * 结果: * java出现了5次 *  * 分析: * 前提:是已经知道了大串和小串。 *  * A:定义一个统计变量,初始化值是0 * B:先在大串中查找一次小串第一次出现的位置 * a:索引是-1,说明不存在了,就返回统计变量 * b:索引不是-1,说明存在,统计变量++ * C:把刚才的索引+小串的长度作为开始位置截取上一次的大串,返回一个新的字符串,并把该字符串的值重新赋值给大串 * D:回到B */public class StringTest4 {public static void main(String[] args) {// 定义大串String maxString = "woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun";// 定义小串String minString = "java";// 写功能实现int count = getCount(maxString, minString);System.out.println("Java在大串中出现了:" + count + "次");}/* * 两个明确: 返回值类型:int 参数列表:两个字符串 */public static int getCount(String maxString, String minString) {// 定义一个统计变量,初始化值是0int count = 0;// 先在大串中查找一次小串第一次出现的位置int index = maxString.indexOf(minString);// 索引不是-1,说明存在,统计变量++while (index != -1) {count++;// 把刚才的索引+小串的长度作为开始位置截取上一次的大串,返回一个新的字符串,并把该字符串的值重新赋值给大串int startIndex = index + minString.length();maxString = maxString.substring(startIndex);// 继续查index = maxString.indexOf(minString);}return count;}}

3、Stringbuffer和StringBuilder

①StringBuffer概述:是一个容器,字符串的组成原理就是通过该类实现的。StringBuffer可以对字符串内容进行增删改查,很多方法和String相同。是一个线程安全的可变字符序列。

②StringBuffer的特点:

  • 长度是可变的。
  • 可以存储不同的数据类型。
  • 最终要转换成字符串进行使用。
  • 可以对字符串进行修改。
③构造方法摘要
  • StringBuffer():构造一个其中不带字符的字符串缓冲区,初始容量为 16 个字符。
  • StringBuffer(CharSequence seq):构造一个字符串缓冲区,它包含与指定的CharSequence 相同的字符。
  • StringBuffer(int capacity):构造一个不带字符,但具有指定初始容量的字符串缓冲区。
  • StringBuffer(String str):构造一个字符串缓冲区,并将其内容初始化为指定的字符串内容。
④StringBuffer常见操作
a、存储功能
  • StringBuffer append():将指定数据作为参数添加到已有数据的结尾处。
  • StringBuffer insert(intoffset ,数据):可以将数据插入到指定offset位置。
b、删除功能
  • StringBufferedelete(start,end):删除缓冲区中的数据,包含start,不包含end。
  • StringBuffer deleteCharAt(index):删除指定位置的字符。
  • 清空缓冲区:对象.delete(0,对象.length());
c、获取
  • 与String相同。
d、反转
  • StringBuffer reverse();
e、将缓冲区中指定数据存储到指定字符数组中
  • voidgetChars(int srcBegin, int srcEnd,char[] dst,int dstBegin)
⑤StringBuilder:一个可变的字符序列。与StringBuffer兼容,但是不保证同步。效率高于Stringbuffer。一般情况下,建议优先使用该类。该类是在JDK1.5以后出现的。它的功能和StringBuffer一致。在这里就不再赘述了。

二、基本数据类型包装类

1、概述

基本数据类型包装类,是为了方便操作基本数据类型值,将其封装成了对象,在对象中定义了属性和行为丰富了该数据的操作,同时描述了该对象的类就称为基本数据类型对象的包装类。

2、基本数据类型对象包装类

byte
ByteshortShortintIntegerlongLongbooleanBooleanfloatFloatdoubleDoublecharCharacter

3、字符串——>基本类型

①使用包装类中的静态方法:xxx.parsexxx(String)

eg:int  parseInt("intString"),八个类型中除了character没有parse方法,其它七种都有。

②如果字符串被Interger进行了对象的封装,可以使用另外一个非静态的方法。intValue()将一个Integer对象转换成基本数据类型。

4、基本类型——>字符串

①基本类型数值+”“;

②用String类中的静态方法valueOf(基本类型数值)

③用Integer的静态方法 valueOf()。

5、JDK1.5新特性:自动拆箱装箱

在JDK1.5之后,有了自动装箱拆箱。

eg:Integer i = new Interger(4);//JDK1.5之前的写法。

       Integer i = 4; //自动装箱,1.5版本以后的写法。

       i = i + 5;//i对象是不能直接和5相加的,其实底层是先将i转成了int类型,再和5相加,而转成int类型的操作是隐式的,这就是自动拆箱,原理就是i.intvalue()。

小知识点:Integer i1 = 100;   Integer i2 = 100;  i1 == i2 ?结果是true。

                  Integer i1 = 200;   Integer i2 = 200;  i1 == i2 ?结果是false。

为什么会出现这样的结果呢?

通过查看内部的代码可知,Integer类有一个缓存,它会缓存-128~127之间的整数,当调用value的时候,不会产生新的对象,而是直接从缓存中取出对象,这样可以提高性能。用构造的时候是肯定会产生新的对象的。

0 0
原创粉丝点击