wangjiedadada  

  1.String类当中常用操作方法

  2.演示

  

  1 package com.example.demo.string;
  2 
  3 import com.sun.media.sound.SoftTuning;
  4 import org.apache.commons.lang.text.StrMatcher;
  5 import org.junit.jupiter.api.Test;
  6 
  7 /**
  8  * 介绍String类当中常用的方法
  9  * <p>
 10  * 常用方法
 11  * 拼接方法
 12  * 替换方法
 13  * 转换方法
 14  * 其他方法
 15  *
 16  * @author wangjei
 17  * @version 1.0
 18  * @date 2021.8.6
 19  * @see {@code String}
 20  */
 21 public class StringDemo {
 22 
 23     @Test
 24     public void stringDemo() {
 25 
 26         /**
 27          * boolean equals(String str);
 28          * boolean equalsIgnoreCase(String str);
 29          * boolean isEmpty();
 30          */
 31         String str = "abcdefghigk";
 32         boolean abc = str.contains ("wangjie");
 33         System.out.println (abc);
 34 
 35         String str1 = "wangjie";
 36         boolean abc1 = str1.equals ("abc");
 37         System.out.println (abc1);
 38         boolean wangjie = str1.equals ("wangjie");
 39         System.out.println (wangjie);
 40         boolean wangjiE = str1.equals ("wangjiE");
 41         System.out.println (wangjiE);
 42         boolean wangjiE1 = str1.equalsIgnoreCase ("wangjiE");
 43         System.out.println (wangjiE1);
 44         boolean wangjiee = str1.equalsIgnoreCase ("wangjiee");
 45         System.out.println (wangjiee);
 46 
 47         String string = "wangjiejie";
 48         System.out.println ("isEmpty:" + string.isEmpty ( ));
 49 
 50         String string1 = "";
 51         System.out.println ("isEmpty:" + string1.isEmpty ( ));
 52 
 53         /**
 54          * char charAt(int index) 返回指定索引处的 char值。
 55          * int length() 返回此字符串的长度。
 56          * int indexOf(String s) 返回指定字符串s第一次出现的字符串内的索引。
 57          * int lastIndexOf(String s) 返回指定字符串s的最后一次出现的字符串中的索引。
 58          */
 59         // * char charAt(int index) 返回指定索引处的 char值。
 60 
 61         String str4 = "wang'jie";
 62         System.out.println ("charAt():" + str4.charAt (0));
 63         // int length() 返回此字符串的长度。
 64         System.out.println ("string():" + str4.length ( ));
 65         //indexof
 66         System.out.println ("indexof():" + str4.indexOf ("e"));
 67         //lastIndexof()
 68         System.out.println ("lastindexof():" + str4.lastIndexOf ("e"));
 69 
 70         /**
 71          * boolean contains(CharSequence s) 当且仅当此字符串包含指定的char值序列时才返回true。
 72          * boolean startsWith(String prefix) 测试此字符串是否以指定的前缀开头。
 73          * boolean endsWith(String suffix) 测试此字符串是否以指定的后缀结尾。
 74          * boolean equals(Object anObject) 将此字符串与指定对象进行比较。
 75          * boolean equalsIgnoreCase(String anotherString) 将此 String与另一个 String比较,忽略案例注意事项。
 76          * boolean isEmpty() 返回 true如果,且仅当 length()为 0
 77          */
 78 
 79         String str5 = "wuyahuimiaodada";
 80         //contains()
 81         System.out.println (str5.contains ("wuyuhui"));
 82         //startwith
 83         System.out.println (str5.startsWith ("wu"));
 84         //endwith
 85         System.out.println (str5.endsWith ("hui"));
 86         //euals
 87         System.out.println ("equals():" + str5.equals ("abc"));
 88         //equalsIgnoreCase()
 89         System.out.println ("equalsIgoreCase():" + str5.equalsIgnoreCase ("abc"));
 90         //isEmpty()
 91         System.out.println ("isEmpty():" + str5.isEmpty ( ));
 92 
 93         /**
 94          * byte[] getBytes() 使用平台的默认字符集将该 String编码为一系列字节,将结果存储到新的字节数组中。
 95          * char[] toCharArray() 将此字符串转换为新的字符数组。
 96          * String toLowerCase() 使用默认语言环境的规则将此 String所有字符转换为小写。
 97          * String toUpperCase() 将此 String所有字符转换为大写,使用默认语言环境的规则。
 98          * static String valueOf(boolean b) 返回 boolean参数的字符串表示形式。
 99          */
100 
101         String str6 = "abdf";
102         byte[] bytes = str6.getBytes ( );
103         System.out.println ("============================");
104         System.out.println ("getBytes():" + str6.getBytes ( ));
105         for (int i = bytes.length - 1; i >= 0; i--) {
106             System.out.println (bytes[i]);
107         }
108         System.out.println ("============================");
109         char[] chars = str6.toCharArray ( );
110         for (char aChar : chars) {
111             System.out.println (aChar);
112         }
113         System.out.println ("==============================");
114 
115         String s = str6.toLowerCase ( );
116         System.out.println ("toLowerCase():" + s);
117 
118         String s1 = str6.toUpperCase ( );
119         System.out.println ("toUpperCase():" + s1);
120 
121         /**
122          *          boolean b = true;
123          *         String s1 = String.valueOf(b);
124          *         System.out.println("s1: " + s1);
125          *
126          *         int number = 100;
127          *         String s2 = String.valueOf(number);
128          *         System.out.println("s2: " + s2);
129          */
130 
131         boolean b = true;
132         String s2 = String.valueOf (b);
133         System.out.println (s2);
134         System.out.println (String.valueOf (b));
135 
136         /**
137          * String replace(CharSequence target, CharSequence replacement) 将与字面目标序列匹配的字符串的每个子字符串替换为指定的文字替换序列。
138          * String[] split(String regex) 将此字符串拆分为给定的 regular expression的匹配。
139          * String substring(int beginIndex) 返回一个字符串,该字符串是此字符串的子字符串。
140          * String substring(int beginIndex, int endIndex) 返回一个字符串,该字符串是此字符串的子字符串。
141          */
142 
143         String string4 = "a,b,c,d";
144         //split()
145         String[] split = string4.split (",");
146         for (String s3 : split) {
147             System.out.println (s3);
148         }
149         //replace()
150         String replace = string4.replace ("a", "wangjei");
151         System.out.println ("replace():" + replace);
152 
153         //substring()
154         String substring = string4.substring (1);
155         System.out.println ("substring:" + substring);
156         //substring(int beginindex,int endindex);
157         String substring1 = string4.substring (0, 3);
158         System.out.println ("substring1:" + substring1);
159 
160 
161         /**
162          * String concat(String str) 将指定的字符串连接到该字符串的末尾。
163          * String trim() 返回一个字符串,其值为此字符串,并删除任何前导和尾随空格。
164          */
165 
166         //concat(Strig str)
167         String string5 = "wangjiesddddddd";
168         String wuyahui = string5.concat ("wuyahui");
169         System.out.println ("concat():" + wuyahui);
170         //trim()
171         String trim = string5.trim ( );
172         System.out.println ("strm():" + trim);
173     }
174 }

补充:

方法 string.valueof(基本数据类型);

api文档截图:

posted on 2021-08-06 10:19  wangjiedadada  阅读(37)  评论(0编辑  收藏  举报