String类

作者: JHW2017 | 来源:发表于2018-07-19 09:45 被阅读0次

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) ;
    }
};

相关文章

  • String

    1.String类2.String类和常量池3.String类常用的方法 1.String类 1.String类又...

  • String

    String UML类图 String 被 final 修饰,String 类不可被继承,不可变类。 String...

  • Java之String类

    String 1. String类基本特性 String类再Java类中也是一个很重要的类,String类的对象是...

  • string类

    string类 1. string类常用方法 2. string类常用方法2 3. string类的查找 判断字符...

  • 二十五、String与StringBuffer/StringBu

    一、 String类 1. String类概述 查阅API中的String类的描述,发现String 类代表字符串...

  • String类概述及其构造方法

    构造方法 String类的判断功能 String类的获取功能 String的转换功能 String类的其他功能

  • Java String总结

    Java String总结 String 类 String类定代码如下: 从代码看有两点需要注意: String类...

  • 8.2-全栈Java笔记:字符串相关类(String/Strin

    字符串相关类(String/StringBuilder /StringBuffer) String类 String...

  • 10.常用API—String

    常用API(String,ArrayList) String String类概述 String类定义的变量可以用于...

  • 第四章 复合类型(3)string类简介

    (三)string类简介 1.string类的优势: 使用string类的时候,必须在头文件中包含string,...

网友评论

      本文标题:String类

      本文链接:https://www.haomeiwen.com/subject/qsszpftx.html