String 类 的 使用

  1 package com.StringUse;
  2 
  3 import java.util.Arrays;
  4 
  5 /*
  6 String 的构造方法:
  7     String()  创建一个空内容 的字符串对象。
  8     String(byte[] bytes)  使用一个字节数组构建一个字符串对象
  9     String(byte[] bytes, int offset, int length) 
 10         bytes :  要解码的数组
 11         offset: 指定从数组中那个索引值开始解码。
 12         length: 要解码多个元素。
 13     String(char[] value)  使用一个字符数组构建一个字符串。    
 14     String(char[] value, int offset, int count)  使用一个字符数组构建一个字符串, 指定开始的索引值,与使用字符个数。
 15     String(int[] codePoints,int offset,int count)
 16     String(String original) 
 17 
 18 记住: 使用字节数组或者字符数组都可以构建一个字符串对象。
 19 
 20 
 21 */
 22 /*public class Stringuse {
 23 
 24     public static void main(String[] args) {
 25         String s = new String();
 26         //String s = "";
 27         byte[] b = {98,99,97};
 28         s = new String(b);
 29         s = new String(b,1,2);
 30         
 31         char[] value = {'情','人','节'};
 32         s = new String(value);
 33         s = new String(value,0,2);
 34         
 35         int[] x ={96,97,98};
 36         s = new String(x,1,2);
 37        System.out.println("字符串的内容是:"+s);
 38     }
 39  
 40 }*/
 41 /*
 42 2.2    获取方法
 43     int length()  获取字符串的长度
 44     char charAt(int index) 获取特定位置的字符 (角标越界)
 45     int indexOf(String str) 查找子串第一次出现的索引值,如果子串没有出现 在字符串中,那么则返回-1表示。
 46     int lastIndexOf(String str) 查找子串最后一次出现的索引值 , 如果子串没有出现 在字符串中,那么则返回-1表示
 47 */
 48 /*public class Stringuse{
 49     public static void main(String[] args) {
 50         String s ="我爱你中国我爱你中国";
 51         System.out.println("字符串的长度:"+s.length());
 52         System.out.println("获取特定位置的字符,"+s.charAt(5));
 53         System.out.println("查找子串第一次出现的索引值,"+s.indexOf("中国"));
 54         System.out.println("查找子串最后一次出现的索引值 ,"+s.lastIndexOf("中国"));
 55     }
 56 }*/
 57 /*
 58 2.3    判断方法
 59     boolean endsWith(String str) 是否以指定字符结束
 60     boolean isEmpty()是否长度为0 如:“” null V1.6
 61     boolean contains(CharSequences) 是否包含指定序列 应用:搜索
 62     boolean equals(Object anObject) 是否相等
 63     boolean equalsIgnoreCase(String anotherString) 忽略大小写是否相等
 64 */
 65 /*public class Stringuse{
 66     public static void main(String[] args) {
 67         String s = "woshihaoren";
 68         System.out.println("是否以指定字符结束,"+s.endsWith("h"));
 69         System.out.println("判断是否长度为空 "+s.isEmpty());
 70         System.out.println("是否包含指定序列 应用:搜索,"+s.contains("hao"));
 71         System.out.println("是否相等"+"woshihaoren".equals(s));
 72         System.out.println("是否相等忽略大小写是否相等"+"Woshihaoren".equalsIgnoreCase(s));
 73     }
 74 }*/
 75 /*2.4    转换方法     
 76 char[] toCharArray()  将字符串转换为字符数组
 77 byte[]    getBytes();//把字符串转字节数组
 78 
 79 字节数组与字符数组、字符串他们三者之间是可以互相转换。
 80 
 81 */
 82 /*public class Stringuse{
 83     public static void main(String[] args) {
 84         String s = "woshihaoren";
 85         char[] ch = s.toCharArray();
 86         System.out.println("将字符串转换为字符数组:"+Arrays.toString(ch));
 87         byte[] b = s.getBytes();//把字符串转字节数组
 88         System.out.println("把字符串转字节数组:"+Arrays.toString(b));
 89     }
 90 }*/
 91 /*
 92 其他方法
 93     String replace(String oldChar, String newChar) 替换
 94     String[] split(String regex) 切割
 95     
 96     String substring(int beginIndex)   指定开始 的索引值截取子串
 97     String substring(int beginIndex, int endIndex)指定开始 与结束的索引值截取子串
 98     
 99     String toUpperCase() 转大写
100     String toLowerCase() 转小写
101     String trim() 去除字符串首尾的空格
102     
103 */
104 public class Stringuse{
105     public static void main(String[] args) {
106         String s = "今天晚上不考试";
107         System.out.println("指定新内容替换旧 的内容:"+s.replace("不", "好好"));
108         String t ="大家-下-无-好";
109         String[] arr = t.split("-");
110         System.out.println("字符串数组的内容:"+ Arrays.toString(arr));
111     }
112 }
原文地址:https://www.cnblogs.com/fujilong/p/4686951.html