Java中关于String类的基本操作(字符与字符串的相互转换、字节与字符串的相互转换、字符串比较、字符串查找、字符串替换、字符串拆分、字符串截取、去掉左右空格、转大小写、判断是否为空)

1 字符与字符串的相互转换

  字符串就是一个字符数组,所以在String类里面支持有字符数组转为字符串以及字符串转为字符的操作方法。

//取得字符串长度:public int length();
//数组的长度:数组名.length;

1.1 将字符数组转为字符串

  用String类的构造方法!

/**
1.  public String(char [] value);将字符数组value中的所有内容变为字符串
2.  public String(char [] value,int offset,int count);将字符数组value中的部分内容变为字符串
offset为开始索引、count为个数

均为成员方法,通过对象调用!!!
*/

public class Test{
    public static void main(String[] args)
    {
        //静态初始化一个字符串数组
        char [] arr = new char[]{'h','e','l','l','o','&','1','3','1','4'};

        //将字符数组中的所有内容变为字符串
        String str1=new String(arr);
        System.out.println(str1);

        //从下表为5的索引开始,将之后的5个字符变为字符串
        String str2=new String(arr,5,5);
        System.out.println(str2);

        //如果,字符的个数超出了数组的范围,运行时会有数组越界异常(StringIndexOutOfBoundsException)
        String str3=new String(arr,5,10);
        System.out.println(str3);

    }
}

在这里插入图片描述

1.2 将字符串转为字符数组

/**
将字符串该为字符数组
1. public char[] tocharArray();
*/
public class Test{
    public static void main(String[] args)
    {
      String str="hello";
      char [] result=str.toCharArray();
      //for-each循环
      for(char i:result)
      {
          System.out.print(i+" ");
      }
    }
}

在这里插入图片描述

1.3 将字符串转为单个字符

/**
1. public char charAt(int index);取得指定索引位置上的字符
index:索引
*/

public class Test{
    public static void main(String[] args)
    {
        //取得索引为0的位置的字符
        System.out.println("hello".charAt(0));
        //如果索引越界,会报数组越界异常StringIndexOutOfBoundsException
        System.out.println("hello".charAt(6));
    }
}

在这里插入图片描述

1.4 判断一个字符串是否由数字组成?

/**
判断一个字符串是否由数组组成
取得字符串长度:public int length();
取得数组的长度:数组名.length
*/


public class Test{
    public static boolean isNumber1(String str)
    {
        for(int i=0;i<str.length();i++)
        {
            //将字符串根据索引转成单个字符
            char result=str.charAt(i);
            if(result<'0'||result>'9')
            {
                return false;
            }
        }
        return true;
    }
    public static boolean isNumber2(String str)
    {
        //将字符串转为字符数组
        char [] data=str.toCharArray();
        for(int i=0;i<data.length;i++)
        {
            if(data[i]<'0'||data[i]>'9')
            {
                return false;
            }
        }
        return true;

    }
    public static void main(String[] args)
    {
      String str="14263";
      System.out.println(isNumber1(str));
      System.out.println(isNumber2(str));
    }
}

在这里插入图片描述

2 字节与字符串的相互转换

2.1 将字节数组转为字符串

  用String类的构造方法!

/**
1. public String(byte[] value);将字节数组中的所有内容转为字符串
2. public String(byte[] value,int offset,int count);将字节数组中的部分内容转为字符串
offset为开始索引,count为个数
 */

public class TestByte{
    public static void main(String[] args)
    {
        byte [] data=new byte[]{1,2,3,4,5};
        //将字节数组中的所有内容变为字符串
        String str1=new String(data);
        System.out.println(str1);
        //将字节数组中的部分内容变为字符串offset为开始点,count为个数
        String str2=new String(data,2,3);
        System.out.println(str2);
        //如果字符的个数超出了数组的索引,会报数组越界异常
        String str3=new String(data,2,4);
        System.out.println(str3);
    }
}

在这里插入图片描述

2.2 将字符串转为字节数组

/**
//将字符串改为字节数组
1. public byte[] getBytes();
 */

public class TestByte{
    public static void main(String[] args)
    {
        byte [] result="HELLO".getBytes();
        for(byte i:result)
        {
            System.out.print(i+" ");
        }
    }
}

在这里插入图片描述
  字节不适合中文,只有字符适合处理中文!

3 字符串比较

3.1 不区分大小相等比较

/**
不区分大小写的相等比较   
  public boolean equalsIgnoreCase(String anotherString)   
 区分大小写        
   public boolean equals(String anotherString)                                                                                      
 */

public class TestByte{
    public static void main(String[] args)
    {
        String str1="Hello";
        //不区分大小写
        System.out.println("hello".equalsIgnoreCase(str1));
        //区分大小写
        System.out.println("hello".equals(str1));
    }
}

在这里插入图片描述

3.2 比较两个字符串的大小

/**
比较两个字符串的大小 
    public int compareTo(String anotherString)    
返回值:(返回值正好是两个字符的差)
① >0  表示该字符串大于比较对象
② <0  表示该字符串小于比较对象
③ =0  表示该字符串等于比较对象                                                                                             
 */

public class TestByte{
    public static void main(String[] args)
    {
     System.out.println("ABCD".compareTo("ABCD"));
     System.out.println("abcd".compareTo("aBcd"));
     System.out.println("abCd".compareTo("abcD"));   
     System.out.println("AB".compareTo("AC"));
    }
}

在这里插入图片描述

4 字符串查找

4.1 判断字符串在源字符串中是否存在

/**
判断字符串str在源字符串中是否存在   
public boolean contains(String str);                                                                                       
 */

public class TestByte{
    public static void main(String[] args)
    {
        String str="helloworld";
        System.out.println(str.contains("hello"));
        System.out.println(str.contains("he"));
        System.out.println(str.contains("hep"));
    }
}

在这里插入图片描述

4.2 判断字符串是否以指定的字符串开始

/**
 判断字符串是否以指定的字符串开始  
public boolean startsWith(String str);                                                                                       
 */

public class TestByte{
    public static void main(String[] args)
    {
        String str="helloworld";
        System.out.println(str.startsWith("hello"));
        System.out.println(str.startsWith("he"));
        System.out.println(str.startsWith("hep"));
    }
}

在这里插入图片描述

4.3 从指定位置开始判断是否以指定的字符串开头

/**
 从指定位置开始判断是否以指定的字符串开头
public boolean startsWith(String str,intdex);                                                                                       
 */
public class TestByte{
    public static void main(String[] args)
    {
        String str="helloworld";
        System.out.println(str.startsWith("hello",0));
        System.out.println(str.startsWith("wo",5));
        System.out.println(str.startsWith("hep",3));
    }
}

在这里插入图片描述

4.4 判断是否以指定的字符串结尾

/**
 判断是否以指定的字符串结尾
public boolean endsWith(String str);                                                                                       
 */
public class TestByte{
    public static void main(String[] args)
    {
        String str="helloworld";
        System.out.println(str.endsWith("hello"));
        System.out.println(str.endsWith("wo"));
        System.out.println(str.endsWith("world"));
        System.out.println(str.endsWith("ld"));
    }
}

在这里插入图片描述

4.5 查找字符串的位置

public int indexOf (String str)从头开始查找指定字符串的位置,查到了返回位置的开始索引,查不到返回-1
public int indexOf (String str,int fromIndex)从指定位置开始查找指定字符串的位置
public int lastIndexOf(String str)从最后一个元素开始由后向前查找字符串的位置
public int lastIndexOf(String str,int fromIndex)从指定位置由后向前查找字符串的位置
public class TestByte{
    public static void main(String[] args)
    {
        String str="helloworld";
        //从头开始查找字符串hlo的位置  没有找到返回-1
        System.out.println(str.indexOf("hlo"));
        //从头开始查找字符串llo的位置  返回2
        System.out.println(str.indexOf("llo"));
        //从下表为4的位置开始,向后找字符串h的位置  没有找到返回-1
        System.out.println(str.indexOf("h",4));
        //从下表为4的位置开始,向后找字符串w的位置 返回5
        System.out.println(str.indexOf("w",4));
        //从最后一个元素开始由后向前查找字符串world的位置,返回5
        System.out.println(str.lastIndexOf("world"));
        //从第4个元素开始由后向前查找字符串world的位置,没有找到返回-1
        System.out.println(str.lastIndexOf("world",4));
        //从第6个元素开始由后向前查找字符串world的位置,找到返回5
        System.out.println(str.lastIndexOf("world",6));
        //从第6个元素开始由后向前查找字符串l的位置,返回3
        System.out.println(str.lastIndexOf("l",6));

    }
}

在这里插入图片描述

5 字符串替换

5.1 替换全部内容

/**
替换全部内容
public String replaceAll(String regex,String replacement)
 */
public class TestReplace{
    public static void main(String[] args)
    {
        String str="hello";
        String result = str.replaceAll("l","*");
        System.out.println(result);
    }
}

在这里插入图片描述

5.2 替换首个内容

/**
替换首个内容
public String replaceFirst(String regex,String replacement)
 */
public class TestReplace{
    public static void main(String[] args)
    {
        String str="hello";
        String result = str.replaceFirst("l","*");
        System.out.println(result);
    }
}

在这里插入图片描述

6 字符串拆分

6.1 将字符串全部拆分

/**
将字符串按照指定格式全部拆分
public String[] split(String regex);
 */

public class TestSplit{
    public static void main(String[] args)
    {
        String str="192 168 106 1";
        String [] result = str.split(" ");
        for(String i:result)
        {
            System.out.println(i);
        }
    }
}
/**
将字符串部分拆分
public String[] split(String regex,int limit);
拆分后数组的长度为limit
 */

public class TestSplit{
    public static void main(String[] args)
    {
        String str="192.168.106.1";
        //转义字符
        String [] result = str.split("\\.");
        for(String i:result)
        {
            System.out.println(i);
        }
    }
}

在这里插入图片描述

6.2 将字符串部分拆分

/**
将字符串部分拆分
public String[] split(String regex,int limit);
拆分后数组的长度为limit
 */

public class TestSplit{
    public static void main(String[] args)
    {
        String str="192 168 106 1";
        String [] result = str.split(" ",3);
        for(String i:result)
        {
            System.out.println(i);
        }
    }
}

在这里插入图片描述

6.3 一个拆分的实例

public class TestSplit{
    public static void main(String[] args)
    {
        String str="苗:20|萌:22|昕:10";
        //需要转义字符
        String [] result = str.split("\\|");
        for(int i=0;i<result.length;i++)
        {
            String [] data = result[i].split(":");
            System.out.println(data[0]+"  "+data[1]);
        }
    }
}

在这里插入图片描述

7 字符串截取

7.1 从指定位置开始截取到文件末尾

 /**
 字符串截取
 public String substring (int beginIndex)从指定位置截取到文件末尾

  */
 public class TestSubString{
     public static void main(String[] args)
     {
        String str="helloworld";
        System.out.print(str.substring(3));
     }
 }

在这里插入图片描述

7.1 从指定位置开始截取部分内容

 /**
 字符串截取
 public String substring (int beginIndex,int endIndex) 从指定位置开始截取部分内容(左闭右开)
  */
 public class TestSubString{
     public static void main(String[] args)
     {
        String str="helloworld";
        System.out.print(str.substring(3,7));
     }
 }

在这里插入图片描述

8 字符串的其他操作

8.1 去掉左右空格,保留中间空格

/**
去掉左右空格,保留中间空格:public String trim();
 */
public class Test2{
    public static void main(String[] args)
    {
        //去掉左右空格保留中间空格
        String str=" ha ha ";
        System.out.println(str.trim());
    }
}

在这里插入图片描述

8.2 转大小写

/**
转大写:public String toUpperCase();
转小写:public String toLowerCase();
 */
public class Test2{
    public static void main(String[] args)
    {
       String str1="abc";
       String str2="AB";
       System.out.println(str1.toUpperCase());
       System.out.println(str2.toLowerCase());
    }
}

在这里插入图片描述

8.3 判断是否为空字符串

/**
判断是否为空字符串:public boolean isEmpty();
 */
public class Test2{
    public static void main(String[] args)
    {
        //只能判断字符串是否是空字符串,而不是null
       String str1="";
       System.out.println(str1.isEmpty());
    }
}

在这里插入图片描述

8.4 首字母大写

public class Test2{
    public static void main(String[] args)
    {
       System.out.println(firstUpper("abcADDFAdie"));
       System.out.println(firstUpper("*bcADDFAdie"));
       System.out.println(firstUpper("苗bcADDFAdie"));
}
    public static  String firstUpper(String str)
    {
        //判断字符串为空
        if(str.isEmpty()||str==null)
        {
            return str;
        }
        return str.substring(0,1).toUpperCase()+str.substring(1);
    }
}

在这里插入图片描述

  • 17
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值