• String 二


     

     

    1.String 类的构造方法

      

    Public String ():空构造

    Public String (Byte [] byte):把字节数组转成字符串

    Public String(Byte[]byte,int index,int length):把字节数组的一部分转出字符串

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

    Public String (char[]value,int index,int count):把字符数组的一部分转成字符串

    Public String (String original):把字符串常量值转成字符串

    1    byte[] bytes={95,96,97,98,99};
    2         String S1=new String(bytes);
    3         //把字节数组转成字符串   运行结果为_`abc
    4         System.out.println(S1);
    5         System.out.println("================");
    6         //从指定位置到指定位置结束,把字节数组转成字符串    运行结果为ab
    7         String S2=new String(bytes,2,2);
    8         System.out.println(S2);

     

     1   char[] arr3 = {'a','b','c'};
     2         String s4 = new String(arr3);
           //把字符数组转成字符串   输出结果为 abc
    3 System.out.println(s4);
    6 char[] arr5 = {'a', 'b', 'c', 'd', 'e', 'f'}; // 0 1 2 3 4 5
          //把字符数组 从指定位置到指定长度的字符 转成字符串   输出结果为 cde 7 String str2 = new String(arr5, 2, 3); 8 System.out.println(str2); 9      10 String str3 = new String("nihao"); 11 System.out.println(str3);

       

    2.String 类的判断功能 

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

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

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

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

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

      Boolean isEmpty();判断字符串是否为空。

      

    1     public static void main(String[]args){
    2         String str = new String("abc");
    3         String str1 = new String("ABC");
    4         //equals 判断字符串内容是否相同,区分大小写;
    5         Boolean a  = str. equals(str1);
    6         System.out.println(a);       输出结果为 false
    1  String str = new String("abc");
    2          String str1 = new String("ABC");
    3          //startsWith  判断字符串中是否以某个指定字符开头
    4          Boolean a  = str.startsWith(str1);
    5          System.out.println(a);       输出结果为  false
    
    
    1   public static void main(String[]args){
    2         String str = new String("abc");
    3         String str1 = new String("ABC");
    4         //equalsIgnoreCase 判断字符串内容是否相同,忽略大小写;
    5         Boolean a  = str. equalsIgnoreCase(str1);
    6         System.out.println(a);        输出结果为   true
    1   public static void main(String[]args){
    2         String str = new String("abc");
    3         String str1 = new String("ABC");
    4         //contains 判断大字符串中是否包含小字符串,
    5         Boolean a  = str.contains(str1);
    6         System.out.println(a);       输出结果为   false
    1  String str = new String("abc");
    2          String str1 = new String("ABC");
    3          //endsWith 判断字符串是否以某个字符串结尾
    4          Boolean a  = str.endsWith(str1);
    5          System.out.println(a)        输出结果为 true

    3.String类的获取功能

      Int length():获取字符串长度

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

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

      Int indexOf(String  str): 返回指定字符串在此字符串中第一次出现处的索引

      Int indexOf (String str,int fromIndex) 返回指定字符串在此字符串中从指定位置后第一次出现处的索引。

      s1.lastIndexOf('a', 7);//从指定位置向前找

      String substring (int start):从指定位置开始截取字符串,默认到末尾。

      String substring(int start ,int end):从指定位置开始到指定位置结束截取字符串。包含头不包含尾,左闭右开

    4.字符串的遍历

    1 String s = "jinghang";
    2 //通过for 循环获取到字符串中每一个字符的索引
    3 for (int i = 0; i < s.length(); i++) {
    4     char c = s.charAt(i);
    5     System.out.print(c+" ");
    6     //通过索引获取每一个字符
    7     System.out.print(s.charAt(i));
    8 }
  • 相关阅读:
    C#实战Microsoft Messaging Queue(MSMQ)
    Spring注解用法
    Last_IO_Errno: 1032
    浅谈TCP/IP网络编程中socket的行为
    Golang网络库中socket阻塞调度源码剖析
    delphi新语法之泛型实现的对象池模板
    Delphi 编码转换 Unicode gbk big5(使用LCMapString设置区域后,再用API转换)
    提高Delphi的编译速度(bpl和bcp)
    解决jqplot与jquery-ui导入必要包时的冲突
    Linux系统下用C语言获取MAC地址
  • 原文地址:https://www.cnblogs.com/xsh726/p/11367968.html
Copyright © 2020-2023  润新知