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课后动手动脑的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Codeforces #440.Div.
- 下一篇: CSS实现自适应不同大小屏幕的背景大图