黑马程序员—JAVA基础—String类、StringBuffer类

来源:互联网 发布:保安巡更软件 编辑:程序博客网 时间:2024/04/28 11:45

-----------android培训java培训、java学习型技术博客、期待与您交流!------------

一、String类的概述和使用

     1多个字符组成的一串数据。

            其实它可以和字符数组进行相互转换。

     2构造方法:

         A:public String()  //空构造

         B:public String(byte[] bytes)//把字节数组转成字符串

        C:public String(byte[] bytes,int offset,int length)//把字节数组的一部分转成字符串

        D:public String(char[] value)//把字符数组转成字符串

        E:public String(char[] value,int offset,int count)/把字符数组的一部分转成字符串

        F:public String(String original)//把字符串常量值转成字符串

Eg

 

<span style="font-family:Arial Black;">/* * 字符串:就是由多个字符组成的一串数据。也可以看成是一个字符数组。 * 通过查看API,可以知道 * A:字符串字面值"abc"也可以看成是一个字符串对象。 * B:字符串是常量,一旦被赋值,就不能被改变。 */public class StringDemo {public static void main(String[] args) {// public String():空构造String s1 = new String();System.out.println("s1:" + s1);System.out.println("s1.length():" + s1.length());System.out.println("--------------------------");// public String(byte[] bytes):把字节数组转成字符串byte[] bys = { 97, 98, 99, 100, 101 };String s2 = new String(bys);System.out.println("s2:" + s2);System.out.println("s2.length():" + s2.length());System.out.println("--------------------------");// public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串// 想得到字符串"bcd"String s3 = new String(bys, 1, 3);System.out.println("s3:" + s3);System.out.println("s3.length():" + s3.length());System.out.println("--------------------------");// public String(char[] value):把字符数组转成字符串char[] chs = { 'a', 'b', 'c', 'd', 'e', '爱', '林', '亲' };String s4 = new String(chs);System.out.println("s4:" + s4);System.out.println("s4.length():" + s4.length());System.out.println("--------------------------");// public String(char[] value,int index,int count):把字符数组的一部分转成字符串String s5 = new String(chs, 2, 4);System.out.println("s5:" + s5);System.out.println("s5.length():" + s5.length());System.out.println("--------------------------");//public String(String original):把字符串常量值转成字符串String s6 = new String("abcde");System.out.println("s6:" + s6);System.out.println("s6.length():" + s6.length());System.out.println("--------------------------");//字符串字面值"abc"也可以看成是一个字符串对象。String s7 = "abcde";System.out.println("s7:"+s7);System.out.println("s7.length():"+s7.length());}}</span>

   3字符串的特点

      A:字符串一旦被赋值,就不能改变。

           注意:这里指的是字符串的内容不能改变,而不是引用不能改变。

     B:字面值作为字符串对象和通过构造方法创建对象的不同

           String s = new String("hello");String s = "hello"的区别?

   4字符串的功能

      A:判断功能

          boolean equals(Object obj) //比较字符串的内容是否相同,区分大小写

          boolean equalsIgnoreCase(String str)//比较字符串的内容是否相同,忽略大小写

          boolean contains(String str)//判断大字符串中是否包含小字符串

          boolean startsWith(String str)//判断字符串是否以某个指定的字符串开头

          boolean endsWith(String str)//判断字符串是否以某个指定的字符串结尾

          boolean isEmpty()//判断字符串是否是空

Eg

 

<span style="font-family:Arial Black;">public class String02 {public static void main(String[] args) {String str1 = "abc";String str2 = "ABC";System.out.println(str1.equals(str2));System.out.println(str1.equalsIgnoreCase(str2));System.out.println(str1 + "包含bc吗?" + str1.contains("bc"));System.out.println(str1 + "是以a开头吗?" + str1.startsWith("a"));System.out.println(str1 + "是空的吗?" + str1.isEmpty());String str3 = null;System.out.println(str3.isEmpty());String str4 = new String();System.out.println(str4.isEmpty());System.out.println(str4);</span>}}

    B:获取功能

        int length()//获取字符串的长度

        char charAt(int index)//获取指定索引位置的字符

        int indexOf(int ch)//返回指定字符在此字符串中第一次出现处的索引

        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)//从指定位置开始到指定位置结束截取字符串

Eg

 

<span style="font-family:Arial Black;">/* * String类的获取功能 */public class StringDemo 1{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()));}}</span>

    C:转换功能

       byte[] getBytes()//把字符串转换为字节数组

       char[] toCharArray()//把字符串转换为字符数组

        static String valueOf(char[] chs)//把字符数组转换成字符串

        static String valueOf(int i)//int类型的数据转成字符串

        String toLowerCase()//把字符串转成小写

        String toUpperCase()//把字符串转成大写

        String concat(String str)//把字符串拼接

Eg

 

<span style="font-family:Arial Black;">/* * String的转换功能: */public class StringDemo2 {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);}}</span>

    D:其他功能

       a:替换功能 

           String replace(char old,char new)

          String replace(String old,String new)

       b:去空格功能

            String trim()

       c:按字典比较功能

            int compareTo(String str)

            int compareToIgnoreCase(String str) 

Eg:

<span style="font-family:Arial Black;">public class StringDemo3 {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}}</span>


二、StringBuffer类

      1概述:

                 字符串做拼接,比较耗时并且也耗内存,而这种拼接操作又是比较常见的,为了 解决这个问题,Java就提供了一个字符串缓冲区类。StringBuffer供我们使用。

      2StringBuffer的构造方法

           A:StringBuffer()

           B:StringBuffer(int size)

          C:StringBuffer(String str)

      3StringBuffer的常见功能

           A:添加功能

          B:删除功能

         C:替换功能

         D:反转功能

         E:截取功能(注意这个返回值)

 

   5面试题

       小细节:

           StringBuffer:同步的,数据安全,效率低。

           StringBuilder:不同步的,数据不安全,效率高。

           A:String,StringBuffer,StringBuilder的区别

           B:StringBuffer和数组的区别?

   6注意的问题:

       String作为形式参数,StringBuffer作为形式参数。

 Eg:

<span style="font-family:Arial Black;">package cn.itcast_StringBufferimport java.util.Scanner;/* * 判断一个字符串是否是对称字符串 * 例如"abc"不是对称字符串,"aba"、"abba"、"aaa"、"mnanm"是对称字符串 *  * 分析: * 判断一个字符串是否是对称的字符串 *         第一个和最后一个比较 * 第二个和倒数第二个比较 * ... * 比较的次数是长度除以2。 */public class StringBufferTest {public static void main(String[] args) {// 创建键盘录入对象Scanner sc = new Scanner(System.in);System.out.println("请输入一个字符串:");String s = sc.nextLine();// 一个一个的比较boolean b = isSame(s);System.out.println("b:" + b);//用字符串缓冲区的反转功能boolean b2 = isSame2(s);System.out.println("b2:"+b2);}public static boolean isSame2(String s) {return new StringBuffer(s).reverse().toString().equals(s);}// public static boolean isSame(String s) {// // 把字符串转成字符数组// char[] chs = s.toCharArray();//// for (int start = 0, end = chs.length - 1; start <= end; start++, end--) {// if (chs[start] != chs[end]) {// return false;// }// }//// return true;// }public static boolean isSame(String s) {boolean flag = true;// 把字符串转成字符数组char[] chs = s.toCharArray();for (int start = 0, end = chs.length - 1; start <= end; start++, end--) {if (chs[start] != chs[end]) {flag = false;break;}}return flag;}}</span>


三、StringBufferStringBuilder/String比较

        A:  StringStringBuffer/Builder区别:

             1、后者相当于容器

              2、字符串拼接 ---- 后者不产生垃圾对象

       B: StringBuffer 与 StringBuilder区别:

            1、 StringBuffer线程安全

             2、 StringBuilder线程不安全

  

-----------android培训java培训、java学习型技术博客、期待与您交流!------------


0 0
原创粉丝点击