日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

04-String课后动手动脑

發布時間:2025/3/15 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 04-String课后动手动脑 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一.String.equals()方法

public final class String

? ? implements java.io.Serializable, Comparable<String>, CharSequence {

/** The value is used for character storage. */

? ? private final char value[];

?/** Cache the hash code for the string */

? ? private int hash; // Default to 0

?/**

?? ? * Initializes a newly created {@code String} object so that it represents

?? ? * an empty character sequence.? Note that use of this constructor is

?? ? * unnecessary since Strings are immutable.

?? ? */

? ? public String() {

? ? ? ? this.value = new char[0];

? ? }

? ? /**

?? ? * Initializes a newly created {@code String} object so that it represents

?? ? * the same sequence of characters as the argument; in other words, the

?? ? * newly created string is a copy of the argument string. Unless an

?? ? * explicit copy of {@code original} is needed, use of this constructor is

?? ? * unnecessary since Strings are immutable.

?? ? *

?? ? * @param? original

?? ? * ? ? ? ? A {@code String}

?? ? */

? ? public String(String original) {

? ? ? ? this.value = original.value;

? ? ? ? this.hash = original.hash;

? ? }

/**

?? ? * Compares this string to the specified object.? The result is {@code

?? ? * true} if and only if the argument is not {@code null} and is a {@code

?? ? * String} object that represents the same sequence of characters as this

?? ? * object.

?? ? *

?? ? * @param? anObject

?? ? * ? ? ? ? The object to compare this {@code String} against

?? ? *

?? ? * @return? {@code true} if the given object represents a {@code String}

?? ? *? ? ? ? ? equivalent to this string, {@code false} otherwise

?? ? *

?? ? * @see? #compareTo(String)

?? ? * @see? #equalsIgnoreCase(String)

?? ? */

? ? public boolean equals(Object anObject) {

? ? ? ? if (this == anObject) {

? ? ? ? ? ? return true;

? ? ? ? }

? ? ? ? if (anObject instanceof String) {

? ? ? ? ? ? String anotherString = (String)anObject;

? ? ? ? ? ? int n = value.length;

? ? ? ? ? ? if (n == anotherString.value.length) {

? ? ? ? ? ? ? ? char v1[] = value;

? ? ? ? ? ? ? ? char v2[] = anotherString.value;

? ? ? ? ? ? ? ? int i = 0;

? ? ? ? ? ? ? ? while (n-- != 0) {

? ? ? ? ? ? ? ? ? ? if (v1[i] != v2[i])

? ? ? ? ? ? ? ? ? ? ? ? return false;

? ? ? ? ? ? ? ? ? ? i++;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? return true;

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? return false;

? ? }

二.String 方法使用說明:

1.Length():返回當前字符串長度

用法:int 變量名=字符串名.length();

2.charAt(int index): ?取字符串中的某一個字符,其中的參數index指的是字符串中序數。字符串的序數從0開始到length()-1。

例:String s=new String(“abcde”); ??能得到s.charAt(4)==’e’;

3.getChars():從這個字符串中的字符復制到目標字符數組

用法:字符串名.getChars()

4.replace(char oldChar,char newChar):將字符串中第一個oldChar替換成newChar.

5.toUpperCase():用于把字符串轉換為大寫。

用法:字符串名.toUpperCase()

6.toLowerCase():方法返回一個字符串,該字符串中的字母被轉換為小寫字母。

用法:字符串名.toLowerCase()

7.trim():調用字符串對象的一個副本,但是所有起始和結尾的空格都被刪除了

例:String s=" Hello World ".trim();就是把"Hello World"放入s中。

8.toCharArrary():將該String對象轉化為char數組

例:char []c=字符串名字.toCharArray();

?

轉載于:https://www.cnblogs.com/mqlblog/p/7742462.html

總結

以上是生活随笔為你收集整理的04-String课后动手动脑的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。