String 中常用的几种方法

/*
String(char[] value)传递字符数组 将字符数组转换为字符串 字符数组不查询编码表
*/
public static void fun1(){
        char[] ch = {'a','b','c','d','e','f'};
        String s= new String(ch);
        String s1= new String(ch,1,2);
        
        
        System.out.println(s1);
    }

/*
String(char[] value,int offeset,int count);
offeset 代表数组开始的索引、count代表个数
将字节转换为字符串
*/
public static void fun2(){
       byte[] bytes={97,98,99,100,101};
       String str=new String(bytes);
        System.out.println(str);
        byte[] bytes1 = {97,98,99,100,101};
         //调用构造方法  传递数组  传递两个int下x,y   代表截取   截取的位置   x代表下标开始的位置  y代表结束位置
        String s = new String(bytes1,1,3);
        System.out.println(s);

}


/*
   int length();   返回的是字符串的长度
*/
     public static void fun3(){
         String s="skjl";
          int i=s.length();
         System.out.println(i);
}


/*
substring(int beginIndex,int endIndex)获取字符串的一部分  包含头 不包含尾   即截取字符串
substring(int beginIndex)  该索引后边全部截取
*/
 public static void fun4(){
         String s="helloword";
         String s2=s.substring(2);
         String s1=s.substring(1,4);
         System.out.println(s1);
         System.out.println(s2);
     }

/*
boolean startaWith(String preFix)  判断一个字符串是否以该字符开头
*/
 public static void fun5(){
         String s="helloworld";
         boolean s1=s.startsWith("hello");
         System.out.println(s1);
     }


/*
判断一个字符串的后缀  结尾 endWith("参数");
*/
 public static void fun6(){
         String s="helloworld";
         boolean s1=s.endsWith("world");
         System.out.println(s1);
     }
/*
contains 判断一个字符是否有另一个字符
*/
 public static void fun7(){
         String s="helloworld";
         boolean s1=s.contains("ll");
         System.out.println(s1);
     }

/*
indexof(String str)   返回int  返回为查找字符的索引 返回-1没有找到 
 
*/
 public static void fun8(){
         String  s="hello.java";
         int s1=s.indexOf('j');
         System.out.println(s1);
     }

/*
将字符串转字节数组 getBytes();以asill码方式输出
*/
 public static void fun9(){
         String s="今天天气真好!";
         byte[] s1=s.getBytes();
         for(int i=0;i<s1.length;i++){
             System.out.println(s1[i]);
         }
     }
/*
将字符串转为字符数组
toCharArray()
原样输出 */ public static void fun10(){ String s="今天天气真好!"; char[] ch=s.toCharArray(); for(int i=0;i<ch.length;i++){ System.out.println(ch[i]); } } /* boolean equals(object obj);判断字符串里边完全相等 返回true s.equalslgnoreCase(object obj); 不区分大小写 的比较 */ public static void fun11(){ String s="hello"; String s1="hELLO"; System.out.println(s.equals(s1)); //false System.out.println(s.equalsIgnoreCase(s1)); //true  } /* 获取首字母方法 charAt(0) substring(0,1) 转成大写字母 toUpperCase() 获取剩余字符串 substring(1) toLowerCase() 转小写 */ public static void main(String[] args) { String s="heLLo"; String s2=s.substring(0,1); System.out.println("heLLo首字母为: "+s2); String s3=s2.toUpperCase(); System.out.println("转为大写字母后为: "+s3); String s4=s.substring(1).toLowerCase(); System.out.println("剩下的字母转换为小写后为: "+s4); System.out.println("最终输出的结果为: "+s3+""+s4); }

 

posted @ 2019-03-18 21:42  芯-觞  阅读(634)  评论(0编辑  收藏  举报