黑马程序员----JAVA----String类常用方法----

时间:2023-02-18 21:49:31

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

 

 

Scanner 包的简单应用:

常用的方法:
             构造器:Scanner in=new Scanner(System.in);
             String next():获取一个单词
             String nextLine():获取一行输入
              int nextInt():获取一个整数

Random:获取生成伪随机数

String:
         String(byte[]):将byte转成字符串
         String(byte[],int offset int length):
         int offset:从哪个角标开始
         int length:一共需要多少个字节

         例如:byte[] by={'1','2','4'};
         String str=new String(by,0,2);

 

    String(char[]):把字符数组转成字符串 
    String(char[],int offset,int length):指定位置,指定长度,转换成字符串

String 实体不可变:
               实体不可变,所有方法(例如替换)只是返回替换后的新串,而原串没有改变

String常量池
              所有的字符串常量都在常量池中,常量池在方法区中。
              当不使用构造器来创建字符串时:
              --查看池中是否存在这个字符串
              --如果有,那么直接返回这个常量字符串的引用。
              --如果不存在,在池中创建这个字符串常量,再返回器引用;

 

 

String方法:判断方法

                   -boolean:   equals(object):将此字符串与指定的对象比较
                   -boolean:   equalsIgnoreCase(String ):将此字符串与另一个字符串相比,忽略大小写
                   -boolean:  contains(String):判断当前字符串是否包含这个字符
                   -boolean:   startWith(String):测试字符串是否以指定的前缀开始
                   -boolean:   startWith(String,int):测试字符串,从指定的索引是否以这个字符串前缀
                   -boolean:   endsWith(String,int):测试此字符串是否以指定的后缀结束。
                   -boolean:   isEmpty():判断是否为空
   

String :获取方法
                   -int   length:返回当前字符串的字符个数
                   -char  charAt(int):返回指定索引下的值
                   -int   indexOf(int):返回指定字符在此字符串中出现的位置
                   -int   indexOf(String str):返回指定字符串在此字符串中出现的第一次位置
                   -int   indexOf(int ch,String formIndex):指定索引开始 字符串第一次出现的位置
                   -int   indexOf(String str,int formIndex):指定索引开始,字符串第一次出现位置

String substring(int):截取字符串,从指定位置到末尾;
String substring(int ,int):指定位置,到指定位置结束;


String:转换方法
                -byte[] getBytes():把字符串转成byte[]返回
                -char[] toCharArray():把字符串转换成字符数组
                -static String copyValueOf(char[] chs):把字符数组转成字符串
                -static String valueOf(char[] chs):把字符数组转成字符串
                -static String valueOf(int);把基本类型转成字符串
                -String toUpper():把字符串转换成大写
                -String toLowerCase():把当前字符串转成小写
                -String concat():连接  等同于 +
 

String方法:其他功能
                 -替换 :String replace(char old,char new)
                  String replace(String old,String new)
                 -分割:String[] split(String)
                 -去除前后空格:trim();


String 比较方法:
               -int compareTo(String): 比较参数字符串和当前字符串谁大谁小(字典顺序)
               -int compareToIgnoreCase(String):忽略大小写比较

 

练习:查找指定字符串在大串中出现的次数!

                   public class IndexOfDemo {
                               public static void main(String[] args) {
                                           //定义一个字符串
                                            String str="abfhelloaddhellokhellojkjhello";
                                           //定义一个子串(要截取的字符串)
                                            String strs="hello";
                                           //定义一个计数器
                                             int count=0;
                                             //循环
                                              while(true){
                                                    //获取指定字符串第一次出现的位置
                                                        int index=str.indexOf(strs);
                                                   //如果等于-1的话,就说明没有这个字符串,直接跳出循环
                                                      if(index==-1){
                                                                   break;
                                                          }
                                                       //如果要是有的话,count就+1
                                                                count++;
                                                        //截取这字符串中的  指定字符串的位置开始  加上这个字符串的长度
                                                                  str=str.substring(index+strs.length());
   
                                                     }
                                                               //将出现的次数打印
                                                            System.out.println(count);
                                 }
}

 

练习:获取已知文件的扩展名

                                   

                       public class Test6 {
                                   public static void main(String[] args){
                                                  //定义String 类型的变量
                                                  String name="abc.java.txt";
                                                   String name1="ab.txt";
                                                 /*
                                                  lastIndexOf返回指定字符在此字符串中最后出现处的索引,
                                                  返回一个Int类型数值,将数值+1
                                                   substring:返回一个新的字符串,该字符串是从指定的索引位置开始,直到字符串结尾,
                                                   */
                                                  String newName=name.substring(name.lastIndexOf(".")+1);
                                                  String newName1=name1.substring(name1.lastIndexOf(".")+1);
                                                  //将获取后的新字符串打印到控制台
                                                  System.out.println("name="+newName);
                                                  System.out.println("name1="+newName1);
                                   }

          }

 

练习:给字符串进行排序

                   public class SortDemo {
                              public static void main(String[] args) {
                                      //定义一个字符串数组
                                      String[] str={"ba","dd","av","dgs"};
                                          //调用排序方法
                                              Sort(str);
                       }
 
                              public static void Sort(String[] str){
                                           for(int i=0;i<str.length;i++){
                                                    for(int j=i+1;j<str.length;j++){
                                                                 //比较方法
                                                                int t=str[i].compareTo(str[j]);
                                                                         if(t>0){
                                                                              //换位
                                                                         String temp=str[i];
                                                                         str[i]=str[j];
                                                                         str[j]=temp;
     
                                   }
                      }
  }
                                               //输出
                                               for(int i=0;i<str.length;i++){
                                                        System.out.print(str[i]+" ");
                                     }
                   }
}                     

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