zoukankan      html  css  js  c++  java
  • 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();

  • 相关阅读:
    ASP.NET学习笔记之VS操作使用技巧
    无线干扰的防止和消减要领总结
    小米蝈蝈 vs 巍
    struts2学习笔记一struts2标签学习
    魏飞
    Hibernate+Mysql在向数据库中保存 / 更新数据时设置编码格式
    程序员爱情 猿女友们,很幸福吗???
    人生
    外链?存储?这个好不好用,fileupyours,fileden,skydrive
    实现网页中增加刷新按钮、链接的方法 搜集
  • 原文地址:https://www.cnblogs.com/mqlblog/p/7742462.html
Copyright © 2011-2022 走看看