欢迎光临散文网 会员登陆 & 注册

java字符串String类用法详解

2022-07-12 17:58 作者:虚云幻仙  | 我要投稿

/**
* String类用法详解
*/

public class TestString {
   public static void main(String[] args) {
       String s1 = "abcde";
       String s2 = s1.substring(1,4);
       //字符串s1的value为{a,b,c,d,e}  .substring()方法用value[1到3] new一个String返回 (1,4)包头1不包尾4
       System.out.println(s2+"的结果为bcd");
       System.out.println((s2==s1)+"的结果为false");
       //s1没有改变还是abcde s1和s2是两个对象
       System.out.println("abcde"=="abcde");
       //常量随类进入方法区的常量池中 常量唯一 直接写"abcde"时永远指向同一个常量
       String s3 = "abc"+"de";
       System.out.println((s1==s3)+"的结果为true");
       //编译阶段会将s3 = "abc"+"de" 变为 s3 = "abcde"
       String s4 = "abc";
       String s5 = "de";
       String s6 = s4+s5;
       System.out.println((s6==s3)+"的结果为false");
       //编译器无法识别两个变量的值 所以s4+s5会new新的String返回
       System.out.println(s6.equals(s3)+"的结果为true");
       //重写Object.equals(Object obj) 如果地址一样则true 地址不同再判断s3 instanceof实例 String类 属于则再判断两个字符串长度是否相同 相同则逐字比较
       //s6 s3指向不同对象 但字符串内容一样 返回true

       String s7 = s4.concat(s5);
       System.out.println(s7+"结果为abcde"+(s7==s6)+"结果为false");
       //s4.concat(s5) Concatenates the specified string to the end of this string. 连接Concatenate指定的specified字符串到this字符串末尾
       //specified指()内的参数列表

       /*
       If the length of the argument string is 0, then this String object is returned.
       argument参数即specified string 如果传参为空字符串则返回this
       Otherwise, a String object is returned that represents a character sequence that is the concatenation of the character sequence represented by this String object and the character sequence represented by the argument string.
       otherwise否则 represent代表 character字符 sequence序列 否则将把this和argument参数 这两个字符串的字符序列连起来返回新的字符串对象
        */

       String s8 = s7.replace('b','d');
       System.out.println(s8+"的结果为adcde");
       String s9 = s8.replace('d','f');
       System.out.println(s9+"的结果为afcfe");
       //.replace(oldChar原本的字符,新的字符) 如果字符串中没有找到oldChar则返回this不替换 如果存在oldChar则将全部oldChar替换

       String s10 = s9.toUpperCase();
       System.out.println(s10+"的结果为AFCFE 全部变成大写");
       String s11 = s10.toLowerCase();
       System.out.println(s11+"的结果为afcfe 全部变为小写");
       System.out.println(s10.equalsIgnoreCase(s11)+"的结果为true 忽略大小写比较内容");
       System.out.println(s10.compareTo(s11)+"的结果为-32");
       //s10按照Unicode字符集对s11进行比较返回int值 返回正数即s10在字典顺序上位于s11后面 返回负数即s10在字典顺序上先于s11
       // 返回两字符串中第一个不同的字符的差 s10.charAt(k)-s11.charAt(k)
       // 如果这样从前往后比较直到一个字符串比完了另一个字符串后面还有字符 则返回两个字符串长度差 s10.length()-s11.length()
       //s10.charAt(0) - s11.charAt(0)  即'A' - 'a' 在Unicode字符集中A位于a前面 所以结果为负数 两个字符在字符集中的位置相差32


       System.out.println(s11.charAt(1)+"的结果为f 返回index索引1对应的字符");
       System.out.println(s11.length()+"的结果为5 返回字符串的长度");

       String s12 = "      a  bcde  ";
       System.out.println(s12.trim()+"的结果为a  bcde");
       //trim修剪 将字符串两端的空格和制表符去掉 中间的空格依然保留

       String s13 = "abdc  ";
       System.out.println(s13.startsWith("ab")+"的结果为true 判断字符串是否以ab开头");
       System.out.println(s13.endsWith("dc")+"的结果为false 判断字符串是否以dc结束 dc后面还有两个空格所以false");
       System.out.println(s13.indexOf(" ")+"的结果为4 从前往后找 返回第一个空格的index下标");
       System.out.println(s13.lastIndexOf(" ")+"的结果为5 从后往前找 返回的index依然是从前往后数的下标");

   }
}

java字符串String类用法详解的评论 (共 条)

分享到微博请遵守国家法律