黑马程序员——String与StringBuffer

来源:互联网 发布:阿里云代理服务器 编辑:程序博客网 时间:2024/05/01 04:58


------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------





1、String类

1、字符串概念

          由多个字符组成的一串数据。
2、构造方法

          创建字符串对象的几种方式

String s = new String();


String s = new String(byte[] bys);  

通过字节数组创建字符串对象常用

String s = new String(byte[] bys,int index, int length)

通过字节数组一部分创建字符串对象

String s = new String(char[] chs)

通过字符数组创建字符串对象常用

String s = new String(char[] chs, int index, int length);

通过字符数组一部分创建字符串对象

String s = new String(String); 

通过给构造方法传入字符串创建字符字符串对象

String s = “helloworld”

直接给字符串对象赋值常用


 

3、字符串的特点及面试题

1、特点及注意事项
          字符串一旦被赋值,就不能改变。
                String s = "hello";
                s += "world";
                System.out.println(s);     //helloworld
          注意:
不可以被改变说的是字符串在常量池中的值,并不是引用s(引用s是可以重新被赋值的)

        字符串其实就是字符数组。

        数组和字符串分别通过什么方式获长度?
            数组:使用的是数组的  .lenght 属性
            字符串:使用的是字符串的 .length()方法

2、
面试题
               a:String s = new String("hello")
String s = "hello"的区别。
                          
答:new String(“hello”)在内存中存在两个对象

                                   “hello”在内存中存在一个对象


               b:请写出结果:
                    String s1 = new String("hello");
                    String s2 = new String("hello");
                    System.out.println(s1==s2);    //false
                    System.out.println(s1.equals(s2));    //true

                    String s3 = new String("hello");
                    String s4 = "hello";
                    System.out.println(s3==s4);    //false
                    System.out.println(s3.equals(s4));    //true

                    String s5 = "hello";
                    String s6 = "hello";
                    System.out.println(s5==s6);   true
                    System.out.println(s5.equals(s6));


             

           c、以下程序运行会出现什么结果

                String s1 = "";

                String s2 = null;

                System.out.println(s1.isEmpty()); //true

                System.out.println(s2.isEmpty()); //NullPointerException空指针异常


                ""会在内存中开辟空间,是一个没有任何数据的对象,而null表示对象根本就不存在,会出现空指针异常。


4、String类——成员方法(掌握-通过Eclipse&API会用即可)

1、判断功能

        注意:字符串进行的任何操作,原字符串都不会改变,操作是新生成的字符串

boolean equals(Object obj)

判断字符串的内容是否相同,区分大小写

boolean equalsIgnoreCase(String str)

判断字符串的内容是否相同,忽略大小写

boolean contains(String str)

判断字符串对象是否包含给定的字符串

boolean startsWith(String str)

判断字符串对象是否是以给定的字符串开始

boolean endsWith(String str)

判断字符串对象是否是以给定的字符串结束

boolean isEmpty()

判断字符串对象是否为空,注意是数据


2、获取功能

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)

返回从指定位置开始到指定位置结束,截取后的字符串原字符串长度不变


3、转换功能

byte[] getBytes()

把字符串转换成字节数组

char[] toCharArray()

把字符串转换成字符数组

static String copyValueOf(char[] chs)

把字符数组转换成字符串

底层调用new String(char[] chs)

static String valueOf(char[] chs)

把字符串数组转换成字符串

底层调用new String(char[] chs)

static String valueOf(int i)

把任意基本类型转换成字符串

String toLowerCase()

把字符变成小写原字符串不变

String toUpperCase()

把字符编程大写原字符串不变

String concat(String str)

字符串链接原字符串不变

 

4、其他功能

String replace(char oldChar,char newChar)

替换功能原字符串不变

String replace(String oldString,String newString)

替换功能原字符串不变

String[] split(String str)

切割功能原字符串不变

String trim()

去除字符串两端空格原字符串长度不变

int compareTo(String str)

字典顺序比较功能

int compareToIgnoreCase(String str)

字典顺序比较功能



5、案例


1、字符串排序

package com.string;public class StringSortTest {//字符串排序public static void main(String[] args) {String str = "zfhijkleyguvbac"; System.out.println(StringSort(str));}public static String StringSort(String str) {char[] ch = str.toCharArray();for (int i = 0; i < ch.length; i++) {for (int j = 0; j < ch.length - i-1 ; j++) {if (ch[j] > ch[j + 1]) {char temp = ch[j];ch[j] = ch[j + 1];ch[j + 1] = temp;}}}return String.valueOf(ch);}public static void print(char[] ch) {for (int i = 0; i < ch.length; i++) {System.out.print(ch[i]);}}private void swap() {}}

2、统计字符串中大写,小写,数字字符出现的次数

 

public class GetTime {public static void main(String[] args) {String str = "hanb678dADFDGgnb1343nbaFGFHq8976baxi";GetTime.getTime(str);}//获取一个字符串中指定子串出现的次数。public static void getTime(String str) {int bigCase = 0;int minCase = 0;int number = 0;char[] ch = str.toCharArray();for (int i = 0; i < ch.length; i++) {if (ch[i] >= 'A' && ch[i] <= 'Z') {bigCase++;} else if (ch[i] >= 'a' && ch[i] <= 'z') {minCase++;} else if (ch[i] >= '0' && ch[i] <= '9') {number++;}}System.out.println("大写字母有:" + bigCase + "个   小写字母有:" + minCase + "个   数字有:" + number + "个");}}

3、获取小字符串在大字符串中出现的次数

 

public class Substring {public static void main(String[] args) {String s = "hellohellollolll";String s1 = "ll";System.out.println(getCount(s, s1));}//获取小字符串在大字符串中出现的次数public static int getCount(String maxStr, String minStr) {int index = maxStr.indexOf(minStr);int count = 0;while (index != -1) {count++;maxStr = maxStr.substring(index + minStr.length());index = maxStr.indexOf(minStr);}return count;}}


2、StringBuffer类

1、概念
     字符串缓冲区类

2、机制
     StringBuffer采用的是缓冲区机制。
     一开始,首先开辟一些空间,然后,随着数据的增多,还可以继续开辟空间。这些操作针对的是同一个对象。
    
    问题:StringBuffer和String的区别?
String 一但被赋值不可以被改变,而StringBuffer是可以被改变的 (因为它是使用了一个缓冲区机制)



4、构造方法

StringBuffer()

初始化一个理论长度为16的空对象

StringBuffer(int capacity)

初始化一个理论长度为指定大小的空对象

StringBuffer(String str)

初始化一个内容为str的对象,理论长度为字符串的长度+ 16


5、成员方法

获取长度

public int length()

字符个数,实际长度

Public int capacity()

字符容量,理论长度

添加功能

StringBuffer append(数据类型 变量名)

在结尾处添加

StringBuffer insert(int index,数据类型 变量名)

在指定位置添加,原数据后移

删除功能

StringBuffer deleteCharAt(int index);

删除指定位置处的字符

StringBuffer delete(int start, int end)

删除指定开始位置和结束位置之间的字符

替换功能

StringBuffer replace(int start, int end, String str)

用指定字符串替start位置到end位置的内容,注意包左不包右

截取功能

public String substring(int start)

从索引为start处截取到结尾

public String substring(int start, int end)

从索引为start,截取到end。包左不包右

反转功能

public StringBuffer reverse()

将此字符序列用其反转形式取代

案例:

public class MethodTest {public static void main(String[] args) {StringBuffer sb = new StringBuffer("helloworldjava");// StringBuffer replace(int start, int end, String str)//  把开始到结束位置的字符用一个新的字符串给替换。sb.replace(2, 3, "ak47");System.out.println(sb);//String substring(int start)://从指定位置到末尾截取System.out.println(sb.substring(2));//String substring(int start, int end): //从指定位置到结束位置截取System.out.println(sb.substring(2,6 ));//StringBuffer reverse()//将此字符序列用其反转形式取代System.out.println(sb.reverse());}}



------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------

 

0 0
原创粉丝点击