String类

2018-07-19  本文已影响0人  JHW2017

1.实例化String对象
直接为String类赋值,字符串是String类匿名对象,若一个字符串已经被一个名称所引用,以后再有相同的声明时,不会再重新开辟空间
,即共享设计,内容重复时,将对象指向已存在的实例空间

public class TestJava {
    public static void main(String[] args) {    
          String name = "LiXingHua";
          System.out.print("姓名:"+“hello".equals("hello"));
    }
}
public class TestJava {
    public static void main(String[] args) {   
        String str1 = "hello";
        String str2 = new String("hello");
        String str3 = "hello";
        System.out.println(str1 == str2);
        System.out.println(str1 == str3);
    }
}

直接调用构造方法,使用new时,无论如何都会再开辟空间

public class TestJava {
    public static void main(String[] args) {    
          String name = new String("lixinghua");
          System.out.print("姓名:"+name);
    }
}

2.判断字符串是否相等,不可用==,其比较的时是地址
可用equals(str)

public class TestJava {
    public static void main(String[] args) {   
        String str1 = "hello";
        String str2 = new String("hello");
        System.out.print(str1.equals(str2));
    }
}

3.字符串一旦声明,不可改变,

public class TestJava {
    public static void main(String[] args) {   
        String str1 = new String("hello");
        String str2 = "hello";
        str1 = str1 + "world";
        str2 = str2 + "world";
        System.out.println(str1);
        System.out.println(str2);
    }
}

上面实际是将str断开在连接,原字符串并未改变。
但可用StringBuffer类完成
4.字符串与字符数组的转换

public class TestJava {
    public static void main(String[] args) {   
        String str1 = new String("hello");
        char c[] = str1.toCharArray();
        for(int i = 0; i < c.length; i++) {
            System.out.print(c[i]+"\t");
        }
        System.out.println("");
        String str2 = new String(c);
        String str3 = new String(c,0,3);
        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str3);
    }
}

5.从字符串取出特定字符

public class TestJava {
    public static void main(String[] args) {   
        String str1 = new String("hello");
        System.out.println(str1.charAt(0));
    }
}

6.与byte数组的转换,与char数组风格类似

public class TestJava {
    public static void main(String[] args) {   
        String str1 = new String("hello");
        byte b[] = str1.getBytes();
        System.out.println(new String(b));
        System.out.println(new String(b,1,3));
    }
}

7.取得字符串长度

str1.length();

8.查找指定字符是否存在

public class StringAPIDemo05{
    public static void main(String args[]){
        String str1 = "abcdefgcgh" ;                // 声明字符串
        System.out.println(str1.indexOf("c")) ;     // 查到返回位置
        System.out.println(str1.indexOf("c",3)) ;   // 查到返回位置,从第4个位置开始查找
        System.out.println(str1.indexOf("x")) ;     // 没有查到返回-1
    }
};

9.去掉左右空格

public class StringAPIDemo06{
    public static void main(String args[]){
        String str1 = "    hello    " ;     // 定义字符串
        System.out.println(str1.trim()) ;   // 去掉左右空格后输出
    }
};

10.字符串截取

public class StringAPIDemo07{
    public static void main(String args[]){
        String str1 = "hello world" ;       // 定义字符串
        System.out.println(str1.substring(6)) ; // 从第7个位置开始截取
        System.out.println(str1.substring(0,5)) ; // 截取0~5个位置的内容
    }
};

11.按指定字符串拆分字符串

public class StringAPIDemo08{
    public static void main(String args[]){
        String str1 = "hello world" ;       // 定义字符串
        String s[] = str1.split(" ") ;      // 按空格进行字符串的拆分
        for(int i=0;i<s.length;i++){        // 循环输出
            System.out.println(s[i]) ;
        }
    }
};

12.字符串大小写转换

public class StringAPIDemo09{
    public static void main(String args[]){
        System.out.println("将\"hello world\"转成大写:" + "hello world".toUpperCase()) ;
        System.out.println("将\"HELLO WORLD\"转成小写:" + "HELLO WORLD".toLowerCase()) ;
    }
};

13.判断是否以指定字符串开头或结尾

public class StringAPIDemo10{
    public static void main(String args[]){
        String str1 = "**HELLO" ;           // 定义字符串
        String str2 = "HELLO**" ;           // 定义字符串
        if(str1.startsWith("**")){          // 判断是否以“**”开头
            System.out.println("(**HELLO)以**开头") ;
        }
        if(str2.endsWith("**")){            // 判断是否以“**”结尾
            System.out.println("(HELLO**)以**结尾") ;
        }
    }
};

14.不区分大小写进行字符串比较

public class StringAPIDemo11{
    public static void main(String args[]){
        String str1 = "HELLO" ;         // 定义字符串
        String str2 = "hello" ;         // 定义字符串
        System.out.println("\"HELLO\" equals \"hello\" " + str1.equals(str2)) ;
        System.out.println("\"HELLO\" equalsIgnoreCase \"hello\" "
                + str1.equalsIgnoreCase(str2)) ;    // 不区分大小写的比较
    }
};

15.将指定字符串替换成其他字符串

public class StringAPIDemo12{
    public static void main(String args[]){
        String str = "hello" ;          // 定义字符串
        String newStr = str.replaceAll("l","x") ;   // 现在将所有的l替换成x
        System.out.println("替换之后的结果:" + newStr) ;
    }
};
上一篇下一篇

猜你喜欢

热点阅读