String类的常用方法

1.1 , 字符数组与字符串

一个字符串可以变成字符数组,同样,一个字符数组也可以变成一个字符串。
在String类中存在以下两种方法:
        将字符串变为字符数组:public char[] toCharArray()
        将字符数组变为字符串:public String(char[] value,int offset,int count)

举例:

public class StringDemo01
{
    public static void main(String[] args)
    {
        String str1 = "Hello" ; //声明字符串并赋值
        char[] c = str1.toCharArray() ; //将字符串变为字符数组,名称为 c
        for(int i=0;i<c.length;i++)     //循环输出
        {
            System.out.print(c[i]+",") ;//字符数组输出
        }
        System.out.println() ;
        String str2 = new String(c) ;   //将字符数组并成字符串
        String str3 = new String(c,0,3) ; //将字符数组变成字符串,从0开始取三个字符
        System.out.println(str2) ;      //输出字符串
        System.out.println(str3) ;      //输出字符串
    }
}

1.2, 从字符串中取得指定位置的字符

如果要在字符串中取得字符,则此方法的返回值是一个char类型。
方法为 : public char CharAt(int index)

public class StringDemo02
{
    public static void main(String[] args)
    {
        String str1 = "Hello" ; //声明String对象,并赋值
        char c = str1.charAt(4) ;//声明Char对象 用于接收返回的字符
        System.out.println(c) ;
    }
}

1.3 , 字符串与字节数组

byte数组(字节数组)在IO操作中经常使用
在String类中提供一下方法用于自付出啊与字节数组中转换。
|-字符串转换为字节数组:public byte[] getBytes()
|-将一个字节数组变为字符串:
|-将全部字节变为String:public String(byte[] bytes)
|-将部分字节变为String:public String(byte[] bytes,int offset,int length)

public class StringDemo03
{
    public static void main(String[] args)
    {
        String str1 = "Hello" ; //声明String对象,并赋值
        byte[] b = str1.getBytes() ; //将字符串变为字节数组
        System.out.println(b) ;
        System.out.println(new String(b)) ;//将全部字节数组变为字符串
        System.out.println(new String(b,2,2)) ;//将部分字节数组变为字符串
    }
}

1.4 , 取得字符串长度

要取得一个字符串长度:public int length()

public class StringDemo04
{
    public static void main(String[] args)
    {
        String str1 = "Hello" ; //声明String对象
        System.out.println("\""+str1+"\"的长度为: "+str1.length()) ;
    }
}

1.5 , 查找指定的字符串是否存在

在实际操作中,经常会使用到判断在一个字符串中是否存在某些内容,可以使用一下方法。
从头开始查找:public int indexOf(String str)
从指定位置开始查找:public int indexOf(String str,int frontIndex)
查找的时候,方法的返回值是一个int类型的数据。此数据表示的是一个字符串的具体位置,如果没有查找到字符串,则返回“-1”

public class StringDemo05
{
    public static void main(String[] args)
    {
        String str1 = "abcdeabcdeabcde" ;   //声明字符串
        System.out.println(str1.indexOf("b")) ;//查到字符串后返回位置
        System.out.println(str1.indexOf("b",2)) ;//查到字符串后返回位置,从第3个开始
        System.out.println(str1.indexOf("z")) ;     //查找未知的字符串 返回“-1”

    }
}

1.6 ,去掉空格

如果一些信息由用户输入的话,有可能出现多余的空格,在这种操作中使用trim()去掉字符串左右的空格,但字符串中间的空格不能去掉。

public class StringDemo06
{
    public static void main(String[] args)
    {
        String str = "   hello   " ;    //声明String对象
        System.out.println(str.trim()) ;//去掉空格
    }
}

1.7 , 字符串截取

从一个指定的字符串中截取内容,使用的方法为:
(1)从指定的位置截取到结束:public String substring(int beginIndex)
(2)截取指定范围的字符串:public String substring(int beginIndex ,int endIndex)

public class StringDemo07
{
    public static void main(String[] args)
    {
        String str = "Hello World" ;    //声明String对象
        System.out.println(str.substring(5)) ;//截取字符串
        System.out.println(str.substring(0,5)) ;//截取字符串
    }
}

1.8 ,拆分字符串

如果现在需要按指定的字符串去拆分一个字符串的话,使用public String[] split(String regex)

public class StringDemo08
{
    public static void main(String[] args)
    {
        String str1 = "hello world" ;   //声明String对象并赋值
        String[] str = str1.split(" "); //按空格字符串拆分字符串
        for(int i=0;i<str.length;i++)   //循环输出
        {
            System.out.println(str[i]) ;
        }
    }
}

1.9,大小写转换

小写变大写: public String toUpperCase()
大写变小写:public String toLowerCase()

public class StringDemo09
{
    public static void main(String[] args)
    {
        System.out.println("\"hello world\"转成大写"+"hello world".toUpperCase()) ; //小写变大写
        System.out.println("\"HELLO WORLD\"转成小写"+"HELLO WORLD".toLowerCase()) ; //大写变小写
    }
}

1.10,判断是否以指定的字符串开头或结尾

在String中可以使用以下两种方法完成:
判断是否以指定的字符串开头:public boolean startsWith(String prifix)
判断是否以指定的字符串结尾:public boolean endsWith(String prifix)

public class StringDemo10
{
    public static void main(String[] args)
    {
        String str1 = "**Hello" ; //声明String对象
        String str2 = "Hello**" ; //声明String对象
        if(str1.startsWith("**")) //判断 str1字符串是否以"**"开头
        {
            System.out.println("**Hello") ;
        }
        if(str2.endsWith("**"))   //判断 str2字符串是否以“**”结尾
        {
            System.out.println("Hello**") ;
        }
    }
}

1.11 ,不区分大小写的比较

在String类中,equals()方法是可以用来进行字符串比较的,但这种方法只针对于字符串大小写一样的情况下,如果需要进行不区分大小写的比较,使用下面方法:
public boolean equalsIgnoreCase(String anotherString)

public class StringDemo11
{
    public static void main(String[] args)
    {
        String str1 = "hello" ; //定义字符串
        String str2 = "HELLO" ; //定义字符串
        System.out.println("\"hello\" equals \"HELLO\""+(str1.equals(str2))) ;
        System.out.println("\"hello\" equalsIgnoreCase \"HELLO\""+(str1.equalsIgnoreCase(str2))) ;
    }
}

1.12 ,字符串替换功能

在String类中使用一下方法进行字符串替换:
public String replaceAll(String regex,String replacement)

public class StringDemo12
{
    public static void main(String[] args)
    {
        String str = "Hello" ; //声明String对象
        String newStr = str.replaceAll("l","o");    //将字符串中"l"换成"o"
        System.out.println(newStr) ;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值