黑马程序员_String

——- android培训java培训、期待与您交流! ———-



1.String类

String类代表的是一个字符串,字符串对象在开发中是最常见的。为了方便我们对象字符串的操作,java就把字符串用对象进行了封装,这个封装类就是String类。


字符串是一个特殊的对象,字符串一旦初始化就不可以被改变

String str = “abc”;

String str1 = new String(“abc”);

上面两个不同之处就是str在内存中只有一个对象,而str2在内存中有两个对象


String类常见构造方法:

String(String original):初始化一个新创建的 String 对象

String(byte[] bytes):byte数组转换为其对应的ASCII码字符串

String(byte[] bytes,int offset,int length):从索引offset开始截取字符串,长度为length

String(char[] value):字符数组转换为字符串

String(char[] value,int offset,int count):从offset开始截取字符串,长度为count



String s1 = "hello";  

String s2 = "hello";  

System.out.println("s1.equals(s2):" +s1.equals(s2));//true  

System.out.println("s1==s2:" + (s1 == s2));//true  

String s3 = new String(“hello”);

String s4 = new String("hello");  

System.out.println("s3.equals(s4):" + s3.equals(s4));//true  

System.out.println("s3==s4:" + (s3 == s4));//false  

System.out.println("s1.equals(s3):" + s1.equals(s3));//true  

System.out.println("s1==s3:" + (s1 == s3));//false  


注:字符串直接赋值地址指向常量池,字符串里有重写的equals方法,用于比较两个字符串内容是否相同,==在比较基本数据类型时是比较两者是否相等,在比较引用类型数据时是比较两者地址值是否相同。

2.String类的常见操作方法

1.获取

|–>字符串中包含的字符数,也就是字符串的长度

int length();获取长度

|–>根据位置获取位置上某个字符

char charAt(int index);

|–>根据字符获取该字符在字符串中的位置

int indexOf(int ch);返回的是ch在字符串中第一次出现的位置

int indexOf(int ch, int formIndex);从formIndex指定位置开始,获取ch在字符串中出现的位置

int indexOf(String str);返回的是str在字符串中第一次出现的位置。

int indexOf(String str, int formIndex);从formIndex指定位置开始,获取str在字符串中出现的位置。

int lastIndexOf(int ch);返回字符串中最后一个出现指定字符的位置。

2.判断

|–>字符串中是否包含某一个子串

boolean contains(str);返回true or false

注意:indexOf(str)也可以判断是否包含:if(str.indexOf(“aa”)!=-1){}而且还可以索引str第一次出现的位置。

|–>字符中是否有内容

boolean isEmpty();原是就是判断长度是否为0

|–>字符串是否以指定内容开头

boolean startsWith(str);

|–>字符串是否以指定内容结尾

boolean endWith(str);

|–>判断字符串内容是否相同,复写了Object类中的equals方法

boolean equals(str);

|–>判断内容是否相同,并忽略大小写

boolean equalsIgnoreCase();

3.转换

|–> 将字符数组转成字符串。

构造函数:String(char[])

String(char[],offset,count):将字符数组中的一部分转成字符串。

静态方法:

static String copyValueOf(char[]);

static String copyValueOf(char[] data, int offset, int count) 

static String valueOf(char[]):

|–>将字符串转成字符数组

char[] toCharArray():

|–>将字节数组转成字符串

String(byte[])

String(byte[],offset,count):将字节数组中的一部分转成字符串。

|–>将字符串转成字节数组。

byte[]  getBytes():

|–>将基本数据类型转成字符串。

static String valueOf(int)

static String valueOf(double)

特殊:字符串和字节数组在转换过程中,是可以指定编码表的。

4.替换

String replace(oldchar,newchar);

String replace(char old,char new)替换一个字符

public static void method_replace(){

String s = "hello java";  



//String s1 = s.replace('q','n');//如果要替换的字符不存在,返回的还是原串。  





String s1 = s.replace("java","world");  

System.out.println("s="+s);  

System.out.println(“s1=”+s1);

}

5.切割

String[] split(regex);

public static void method_split() {

String s = "zhagnsa,lisi,wangwu";  



String[] arr  = s.split(",");//进行切割,返回数组  



for(int x = 0; x<arr.length; x++){  //运用for循环对字符串数组进行遍历

    System.out.println(arr[x]);  

}  

}

6.子串

String substring(begin);

String substring(begin,end);

代码示例:

public static void method_sub(){

String s = "abcdef";  



System.out.println(s.substring(2));//从指定位置开始到结尾。如果角标不存在,会出现字符串角标越界异常。  

System.out.println(s.substring(2,4));//包含头,不包含尾。s.substring(0,s.length());  

}

7.转换,去除空格,比较

|–>将字符串转成大写或则小写。

String toUpperCase();

String toLowerCase();

|–>将字符串两端的多余空格去除。

String trim();

|–>对两个字符串进行自然顺序的比较。

int compareTo(string);

判断两个字符串中的最大相同子串

class StringTest {

public static String getMaxSubString(String s1,String s2) {  

    //建立两个对象,分别存储较大与较小的字符串  

    String max = "",min = "";  



    max = (s1.length()>s2.length())?s1: s2;//三元运算符  

    min = (max==s1)?s2: s1;  



    for(int x=0; x<min.length(); x++) {  //外循环控制最大对比次数

        for(int y=0,z=min.length()-x; z!=min.length()+1; y++,z++){                  //内循环控制字符串长度   

            String temp = min.substring(y,z);//获取指定长度字符串

if(max.contains(temp))

                return temp;返回结果  

        }  

    }  

    return "";  

}  



public static void main(String[] args) {  

    String s1 = "ab";  

    String s2 = "cvhellobnm";  

    sop(getMaxSubString(s2,s1));  

}  



public static void sop(String str)  {  

    System.out.println(str);  

}  

}

3.StringBuffer

StringBuffer是字符串缓冲区,是一个容器。

特点:

1.长度是可变化的。

2.可以字节操作多个数据类型。

3.最终会通过toString()方法变成字符串。

4.可以对字符串内容进行增删

StringBuffer常见操作

1.存储:

StringBuffer append():将指定数据作为参数添加到已有数据结尾处。

StringBuffer insert(index,数据):可以将数据插入到指定index位置。

2.删除:

StringBuffer delete(start,end):删除缓冲区中的数据,包含start,不包含end。

StringBuffer deleteCharAt(index):删除指定位置的字符。

3.获取:

char charAt(int index) 

int indexOf(String str) 

int lastIndexOf(String str) 

int length() 

String substring(int start, int end) 

4.修改:

StringBuffer replace(start,end,string);

void setCharAt(int index, char ch) ;

5.反转:

StringBuffer reverse();

6.将缓冲区中指定数据存储到指定字符数组中:

void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 

JDK1.5 版本之后出现了StringBuilder.

StringBuffer是线程同步。

StringBuilder是线程不同步,速度更快。

以后开发,建议使用StringBuilder

4.基本数据类型对象包装类

byte———Byte

short———short

int———–Integer

long———–Long

Boolean———–Boolean

float———–Float

double———–Double

char———–Character

基本数据类型对象包装类的最常见作用就是用于基本数据类型和字符串类型之间做转换:

基本数据类型转成字符串:

1.基本数据类型+””:

2.基本数据类型.toString(基本数据类型值);如:Inter.toString(50);.//将50整数变成”50”

字符串转成基本数据类型:

格式:xxx a = Xxx.parseXxx(String);

int a = Integer.parseInt(“111”);

double b = Double.parseDouble(“12.36”);

boolean b = Boolean.parseBoolean(“true”);

Integer i = new Integer("123");

int num = i.intValue();

十进制转成其他进制:

|--->toBinaryString(string);

|--->toHexString(string);

|--->toOctalString(string);

如:System.out.prinln(Integer.toBinaryString(6));

其他进制转成十进制:

parseInt(string,radix);

如:int x = Integer.parseInt(“3c”,16);

JDK1.5版本之后出现的新特性,简化了定义方式:

Integer x = new Integer(4);可以直接写成Integer x = 4;//自动装箱。

x = x + 5;//自动拆箱。通过intValue方法。l

注意:在使用时,Integer x = null;上面的代码就会出现NullPointerException。

代码示例:

class IntegerDemo {

public static void main(String[] args) {  



  // Integer x = new Integer(4);  



    Integer x = 4;//自动装箱,和上面那个一样//new Integer(4)  



    x = x/* x.intValue() */ + 2;//x+2:x 进行自动拆箱。变成成了int类型。和2进行加法运算。  

                //再将和进行装箱赋给x。  



    Integer m = 128;  

    Integer n = 128;  



    sop("m==n:"+(m==n));//结果为false,因为是两个不同的对象  



    Integer a = 127;  

    Integer b = 127;  



    sop("a==b:"+(a==b));//结果为true。因为a和b指向了同一个Integer对象。  

     //因为当数值在byte范围内容,对于新特性,如果该数值已经存在,则不会在开辟新的空间。  

}  

public static void method(){

    Integer x = new Integer("123");  



    Integer y = new Integer(123);  



    sop("x==y:"+(x==y));//false,两个不同的对象  

    sop("x.equals(y):"+x.equals(y));//true,Integer复写了equals的方法,是比较两个对象的数值是否相同  

}  



public static void sop(String str){  

    System.out.println(str);  

}  

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值