zoukankan      html  css  js  c++  java
  • Java常用类之String

    String 类:

      1. java.lang.String 类代表不可变的字符序列;

        2. “XXX” 为该类的一个对象;

          3. String 类的常用构造方法:

        ① String(String original):初始化一个新创建的 String 对象,使其表示一个与参数相同的字符序列;换句话说,新创建的字符串是该参数字符串的副本。

               ② String(char[] value):分配一个新的 String,使其表示字符数组参数中当前包含的字符序列。

               ③ String(char[] value, int offset, int count):分配一个新的 String,它包含取自字符数组参数一个子数组的字符。 

         Demo_1

    public class Test {
    	public static void main(String[] args){
    		String s1 = "hello"; 
    		String s2 = "hello";
    		System.out.println(s1 == s2); // true
    		// java的编译器已经优化了, 在 DataSegment中已经存在hello,不会再分配一块存写hello.
    		String s3 = new String("hello");
    		String s4 = new String("hello");
    		System.out.println(s3 == s4); //false
    		System.out.println(s3.equals(s4)); // true
    		char c[] = {'s','u','n',' ','j','a','v','a'};
    		String s5 = new String(c);
    		System.out.println(c.equals(s5));// false
    		String s6 = new String(c,4,4);
    		System.out.println(s5); // sun java
    		System.out.println(s6); // java
    	}
    }
    

     // 若 String 类没有改写java.lang.object 中的 equals,则指的是同一个对象,而 “s1==s2” 就是没有改写,尽管内容一样,但不是同一个对象。

       4. String 类的常用方法_1:

         (1) public char charAt(int index):返回指定索引处的 char 值。索引范围为从 0length() - 1

                (2) public int length():返回此字符串的长度

                (3) public int indexOf(String str):如果字符串参数作为一个子字符串在此对象中出现,则返回第一个这种子字符串的第一个字符的索引;如果它不作为一个子字符串出现,则返回 -1

                (4) public int indexOf(String str, int fromIndex):指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始,否则返回-1

                (5) public boolean equalsIgnoreCase(String anotherString):将此 String 与另一个 String 比较,不考虑大小写。如果两个字符串的长度相同,并且其中的相应字符都相等(忽略大小写),则认为这两个字符串是相等的。如果参数不为 null,且这两个 String 相等(忽略大小写),则返回 true;否则返回 false

                (6) public String replace(char oldChar, char newChar):返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的

         Demo_2

    public class Test {
    	public static void main(String[] args) {
    		String s1 = "sun java";
    		String s2 = "Sun Java";
    		System.out.println(s1.charAt(1)); // u
    		System.out.println(s2.length()); // 8
    		System.out.println(s1.indexOf("java")); // 4
    		System.out.println(s1.indexOf("Java")); // -1
    		System.out.println(s1.equals(s2)); // false
    		System.out.println(s1.equalsIgnoreCase(s2)); // true
    		String s3 = "你是一只小鸟,你在飞";
    		String s4 = s3.replace("你", "我");
    		System.out.println(s4); // 我是一只小鸟,我在飞
    	}
    }
    

       5. String 类的常用方法_2:

        (1) public boolean startsWith(String prefix):测试此字符串是否以指定的前缀开始

               (2) public boolean endsWith(String suffix):测试此字符串是否以指定的后缀结束

               (3) public String toLowerCase():使用默认语言环境的规则将此 String 中的所有字符都转换为小写。这等效于调用 toLowerCase(Locale.getDefault())

               (4) public String toUpperCase():使用默认语言环境的规则将此 String 中的所有字符都转换为大写。此方法等效于 toUpperCase(Locale.getDefault())

               (5) public String substring(int beginIndex):返回一个新的字符串,它是此字符串的一个子字符串。该子字符串从指定索引处的字符开始,直到此字符串末尾

               (6) public String substring(int beginIndex, int endIndex):返回一个新字符串,它是此字符串的一个子字符串。该子字符串从指定的 beginIndex 处开始,直到索引 endIndex - 1 处的字符。因此,该子字符串的长度为 endIndex-beginIndex

               (7) public String trim():返回字符串的副本,忽略前导空白和尾部空白

         Demo_3

    public class Test {
    	public static void main(String[] args) {
    		String s1 = "You Are A Good Boy!!!";
    		String s2 = "  sun  java!!!   ";
    		System.out.println(s1.startsWith("You")); // true
    		System.out.println(s1.endsWith("Boy")); // false
    		String s1L = s1.toLowerCase();
    		String s1U = s1.toUpperCase();
    		System.out.println(s1L); // you are a good boy!!!
    		System.out.println(s1U); // YOU ARE A GOOD BOY!!!
    		String subs = s1.substring(10); // 
    		String sp = s2.trim(); 
    		System.out.println(subs); // Good Boy!!!
    		System.out.println(sp); //sun  java!!!
    	}
    }
    

     6. String 类的常用方法_3:

      (1) 静态重载方法

               public static String valueOf(...)  // 可以将基本类型数据转换为字符串

               6.1  public static String valueOf(boolean b):返回 boolean 参数的字符串表示形式

               6.2  public static String valueOf(char c):返回 char 参数的字符串表示形式

               6.3  public static String valueOf(int i): 返回 int 参数的字符串表示形式

               6.4  public static String valueOf(long L):返回 long 参数的字符串表示形式

               6.5  public static String valueOf(float f):返回 float 参数的字符串表示形式

               6.6  public static String valueOf(double d):返回 double 参数的字符串表示形式

         Demo_4

    public class Test {
    	public static void main(String[] args) {
    		int i = 12345678;
    		String s1 = String.valueOf(i);
    		System.out.println("j是"+s1.length()+"位数"); // j是8位数
    		String s2 = "Mary,Fa,1976";
    		String s3[] = s2.split(",");
    		System.out.println(s3.length); // 3
    		for(int j=0;j<s3.length;j++){
    			System.out.print(s3[j]+"  "); // Mary  Fa  1976  
    		}
    	}
    }
    
  • 相关阅读:
    spring mvc文件上传,request对象转换异常
    解决国内android sdk无法更新,google不能的简单办法
    1.5 高速找出机器故障
    图的深度优先搜索与广度优先搜索
    我对ThreadLocal的理解
    Leetcode--3Sum
    &lt;转&gt;Openstack Ceilometer监控项扩展
    怎样利用WordPress创建自己定义注冊表单插件
    java_免费视频课程汇总
    HDU 3641 Pseudoprime numbers(快速幂)
  • 原文地址:https://www.cnblogs.com/bosongokay/p/6757042.html
Copyright © 2011-2022 走看看