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

 

posted on 2017-10-27 11:49  孟庆淋  阅读(136)  评论(0编辑  收藏  举报

导航