Java-String类

1.String.equals()

①   当比较两个对象时,用”==”返回false,用equals函数返回true

eg:

String s1=new String(“Hello!”);

String s2=new String(“Hello!”);

System.out.println(s1==s2);//false

System.out.println(s1.equals(s2));//true

原因:两个String类型的变量s1和s2都通过new关键字分别创建了一个新的String对象,这个new关键字为创建的每个对象分配一块新的、独立的内存。因此当通过"=="来比较它们所引用的是否是同一个对象时,将返回false。而通过equals()方法来比较时,则返回true。

②   比较同一对象时,“==”与equals函数的结果相同

eg:

String s1=new String(“java”);

String s2=s1;

System.out.println(s1==s2);//true

System.out.println(s1.equals(s2));//true

原因:虽然变量s1通过new关键字来创建了一个String对象,但是s2是由s1赋值而来,即把s1的引用赋值给了s2,所以s2所引用的对象其实就是s1所引用的对象。当通过"=="来比较时,返回true。

③   当String作为基本数据类型来使用时,“==”和equals函数结果相同

eg:

String s1=”Hello!”;

String s2=”Hello!”;

System.out.println(s1==s2);//true

System.out.println(s1.equals(s2));//true

原因:String对象是作为一个基本类型来使用的,虚拟机不会为这两个String对象分配新的内存堆,而是到String缓冲池中来寻找。此时,s1和s2所引用的是同一个对象。

2.String.length()

语法:public int length()

功能:此方法返回由该对象表示的字符序列的长度。

eg:

String str=”dwfsdfwfsadf”;

System.out.println("length of string =”+str.length());

运行结果为:

length of string = 15

3.String. charAt()

语法:public charAt(int index)

功能:用于返回index处的字符。

eg:

String s = "www.runoob.com";

char result = s.charAt(8);

System.out.println(result);

运行结果为:

o

4.String.getChars()

语法:public void getChars(int start,int end,char c[],int offset)

功能:将当前字符串从start到end-1位置上的字符复制到字符数组c中,并从c的offset处开始存放

eg:

String str = "abcdefghikl";

Char[] ch = new char[8];

str.getChars(2,5,ch,0);

System.out.println(ch); 

运行结果为:

cde

5.String.replace()

语法:public String replace(char oldChar, char newChar)

功能:通过用 newChar 字符替换字符串中出现的所有 oldChar 字符,并返回替换后的新字符串。

eg:

String Str = new String("hello");

 System.out.print("返回值 :" );

 System.out.println(Str.replace('o', 'T'));

运行结果为:

返回值:hellT

6.String.toUpperCase()

语法:public String toUpperCase()

功能:转换为大写的字符串。

eg:

System.out.println(new String(“hello”).toUpperCase());

运行结果为:

HELLO

7.String.toLowerCase()

语法:public String toLowerCase()

功能:将字符串全部转换成小写

eg:

System.out.println(new String(“HELLO”).toLowerCase());

运行结果为:

hello

8.String. trim()

语法:public String trim()

功能:返回一个字符串副本,并忽略(去除)开头和结尾的空白

eg:

String x=“  abc def  ”;

 System.out.println(x.trim());

运行结果为:

abc def

9.String. toCharArray()

语法:public char[] toCharArray()

功能:将字符串转换成一个新的字符数组

eg:

String s1=new String("Hello World!");

 char[] c=s1.toCharArray();

System.out.println("数组c的长度为:"+c.length);

System.out.println("c[4]:"+c[4]);

运行结果为:

数组c的长度为:12

c[4]:o

 

10.实现“级联”

程序源代码

public class MyCounter {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Counter counter1=new Counter(1);//定义对象counter1
        System.out.println("counter1:"+counter1.GetNumber());//显示counter1的number值
        Counter counter2=counter1.increase(100).decrease(2).increase(3);//定义对象counter2
        System.out.println("counter2:"+counter2.GetNumber());//显示counter2的number值

    }

}

class Counter
{
    int number;
    
    
    public int GetNumber()
    {
        return number;//返回number的值
    }
    
    public Counter(int number)//构造参数为number赋值
    {
        this.number=number;
    }
    
    public Counter()//空构造函数
    {
        
    }
    
    
    public Counter increase(int i)//加函数
    {
        Counter counter1=new Counter();//定义对象
        counter1.number=i+number;
        return counter1;//返回对象
        
    }
    
    public Counter decrease(int i)
    {
        Counter counter1=new Counter();
        counter1.number=number-i;
        return counter1;
    }
    
    //要想实现“级联”调用特性,需要让定义的方法返回一个对象,这样才可以再继续调用其函数
}

结果截图

 

转载于:https://www.cnblogs.com/qilin20/p/7737624.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值