重新开始第5天

String

String s1 = "hello world";
       byte[] bytes = {97,98,99};
       String s2 = new String(bytes);//将bytes数组转换成字符串
       String s3 = new String(bytes,1,2);
       //字节数组,数组元素下标的起始位置,长度
       System.out.println(s3);

方法

public class ReplayTest {
   public static void main(String[] args) {
       //String类中常用方法
       char c = "中国人".charAt(1);
       System.out.println(c);//国
​
       //int compareTo(String anotherString) 比较
       int result = "abc".compareTo("abc");
       System.out.println(result);//0(等于0)前后一致
​
       int result2 = "abcd".compareTo("abce");
       System.out.println(result2);//-1(小于0)前小后大
​
       int result3 = "abce".compareTo("abcd");
       System.out.println(result3);//1(大于0)前大后小
​
       System.out.println("xyz".compareTo("yxz"));//-1
​
       //boolean contains(CharSequence s) //判断前面的字符串中是否包含后面的子字符串
       System.out.println("HelloWorld.java".contains(".java"));//true
       //包含为true不包含为false
​
       //boolean endsWith(String suffix)
       //判断当前字符串是否以某个字符串结尾
       System.out.println("tese.txt".endsWith(".java"));//false
       System.out.println("tese.txt".endsWith(".txt"));//true
​
       //boolean equals(Object anObject)  
       //比较两个字符串是否相等
       System.out.println("abe".equals("abc"));//true
​
       //boolean equalsIgnoreCase(String anotherString)  
       //判断两个字符串是否相等,并且忽略大小写
       System.out.println("AbC".equalsIgnoreCase("abc"));//true
​
       //byte[] getBytes()  
       //将字符串对象转换成字节数组
       byte[] bytes = "abc".getBytes();
       for (int i = 0; i < bytes.length ; i++) {
           System.out.println(bytes[i]);//97,98,99
      }
​
       //int indexOf(String str)  
       //判断某个字符串在当前字符串中第一次出现的索引(下标)
       System.out.println("abcdsafdsafd".indexOf("d"));//3
​
       //boolean isEmpty()  
       //判断某个字符串是否为空
       String  s = "a";
       System.out.println(s.isEmpty());//false
​
       //int length()  
       //字符串长度
       //判断数组长度是length属性,判断段字符串长度是length()方法
       System.out.println("accd".length());//4
​
       //int lastIndexOf(String str)  
       //判断某个字符串在当前字符串中最后一次出现的索引(下标)
       System.out.println("fjdafldbnaognoeiw".lastIndexOf("bn"));//7
​
       //String replace(CharSequence target, CharSequence replacement)  
       //替换
       String newstring = "www.baidu.com".replace("baidu","aiqiyi");
       System.out.println(newstring);
​
       //String[] split(String regex)  
       //拆分字符串
       String[]  mmd = "1900-10-11".split("-");//以-分隔符进行拆分
       for (int i = 0; i < mmd.length ; i++) {
           System.out.println(mmd[i]);
      }
​
       //boolean startsWith(String prefix)  
       //判断某个字符串是否以这个字符串开始
       System.out.println("www.baidu.com".startsWith("www"));//true
​
​
       //String substring(int beginIndex)  
       //截取字符串
       System.out.println("www.baidu.com".substring(5));//aidu.com
       //beginIndex起始位置(包括)
       //endIndex结束位置(不包括)
       //substring(int beginIndex,int endIndex)
       System.out.println("www.baidu.com".substring(4,7));//bai
​
​
       //将字符串转换成char数组
       //char[] toCharArray()
       char[] chars = "我是中国人".toCharArray();
       for (int i = 0; i < chars.length ; i++) {
           System.out.println(chars[i]);
      }
       //   String toLowerCase();//
       //转换为小写
       System.out.println("fjlkdsajflkdajslk".toLowerCase());
       //String toUpperCase();
       //转换为大写
       System.out.println("sjdfojasoi".toUpperCase());
​
       //String trim()  
       //取出字符串空白,取出前空白和后空白
       System.out.println("     1     2     ".trim());
​
       //valueOf
       //将非字符串转换成字符串
       String s1 = String.valueOf(3.13);
       System.out.println(s1);
  }
}
​

public class ReplayTest {
   public static void main(String[] args) {
     //如果以后需要进行大量字符串的拼接操作,建议使用JDK自带的
     //java.lang.StringBuffer
     //java.lang.StringBuild
     StringBuffer stringBuffer = new StringBuffer();
     stringBuffer.append("w");
     stringBuffer.append("w");
     stringBuffer.append("w");
     stringBuffer.append(".");
     stringBuffer.append("b");
     stringBuffer.append("ai");
     stringBuffer.append("du.co");
     stringBuffer.append("m");
       System.out.println(stringBuffer);
       //StringBuilder中的方法都有:synchronized关键字修饰,线程安全
       //StringBuilder中的方法没有:synchronized关键字修饰,线程不安全
       StringBuilder stringBuilder = new StringBuilder();
       stringBuilder.append("小");
       stringBuilder.append("鸟");
       stringBuilder.append("坐");
       stringBuilder.append("上");
       stringBuilder.append("了");
       stringBuilder.append("大");
       stringBuilder.append("飞");
       stringBuilder.append("鸡");
       System.out.println(stringBuilder);
​
  }
}

 

 

posted @ 2020-09-23 20:39  锁猴  阅读(294)  评论(0)    收藏  举报