java基础知识之四:String的基本用法

  本文来介绍Java中的String,什么是String呢,字符串就是一序列字符组成的。Java中用关键字String表示字符串对象,严格来说,String时候对象,而不是变量类型。在自动化测试过程中,经常需要用到String对象,特别是断言的部分,需要进行字符串匹配判断。下面的例子,介绍了几个String基本的属性和方法。

    package lessons;  
      
    public class MyClass {  
          
        public static void main(String[] args) {  
              
            // 用关键字String来定义一个字符串变量  
            String myString = "Hello a world abc";  
            //计算字符长度  
            System.out.println(myString.length());  
            //转换小写  
            System.out.println(myString.toLowerCase());  
            //转换大写  
            System.out.println(myString.toUpperCase());  
            //字符串用加号连接  
            String st1 = "Hello";  
            String st2 = "world";  
            System.out.println(st1 + st2);  
            //替换字母  
            System.out.println(myString.replace('a', 'Y'));  
            //查找某一个字母的索引  
            System.out.println(myString.indexOf('w'));  
            //字符串切割  
            System.out.println(myString.substring(1, 9)); //从左到右,包括索引1,但是右边界不包含索引9  
            System.out.println(myString.substring(4)); //从索引为4,包含4开始切割,4之前的不要  
          
        }  
    } 

猜你喜欢

转载自www.cnblogs.com/jshtest/p/9150235.html