码迷,mamicode.com
首页 > 编程语言 > 详细

《Just For Java——基础扎实》——第二节:String

时间:2015-07-25 22:55:50      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

一、String简介

  包:java.lang.String,java.lang提供利用 Java 编程语言进行程序设计的基础类。

  实现:public final class String  implements java.io.Serializable, Comparable<String>, CharSequence{ }

  其中:java.io.Serializable  是序列化有关的接口;

       java.lang.CharSequence  是cahr值的一个可读序列;

     java.lang.Comparable<String>  强行对实现它的类的每个对象强行进行自然排序。 

  类简介:

/**
 * The <code>String</code> class represents character strings. All
 * string literals in Java programs, such as <code>"abc"</code>, are
 * implemented as instances of this class.
 * <p>
 * Strings are constant; their values cannot be changed after they
 * are created. String buffers support mutable strings.
 * Because String objects are immutable they can be shared. For example:
 * <p><blockquote><pre>
 *     String str = "abc";
 * </pre></blockquote><p>
 * is equivalent to:
 * <p><blockquote><pre>
 *     char data[] = {‘a‘, ‘b‘, ‘c‘};
 *     String str = new String(data);
 * </pre></blockquote><p>
 * Here are some more examples of how strings can be used:
 * <p><blockquote><pre>
 *     System.out.println("abc");
 *     String cde = "cde";
 *     System.out.println("abc" + cde);
 *     String c = "abc".substring(2,3);
 *     String d = cde.substring(1, 2);
 * </pre></blockquote>
 * <p>
 * The class <code>String</code> includes methods for examining
 * individual characters of the sequence, for comparing strings, for
 * searching strings, for extracting substrings, and for creating a
 * copy of a string with all characters translated to uppercase or to
 * lowercase. Case mapping is based on the Unicode Standard version
 * specified by the {@link java.lang.Character Character} class.
 * <p>
 * The Java language provides special support for the string
 * concatenation operator (&nbsp;+&nbsp;), and for conversion of
 * other objects to strings. String concatenation is implemented
 * through the <code>StringBuilder</code>(or <code>StringBuffer</code>)
 * class and its <code>append</code> method.
 * String conversions are implemented through the method
 * <code>toString</code>, defined by <code>Object</code> and
 * inherited by all classes in Java. For additional information on
 * string concatenation and conversion, see Gosling, Joy, and Steele,
 * <i>The Java Language Specification</i>.
 *
 * <p> Unless otherwise noted, passing a <tt>null</tt> argument to a constructor
 * or method in this class will cause a {@link NullPointerException} to be
 * thrown.
 *
 * <p>A <code>String</code> represents a string in the UTF-16 format
 * in which <em>supplementary characters</em> are represented by <em>surrogate
 * pairs</em> (see the section <a href="Character.html#unicode">Unicode
 * Character Representations</a> in the <code>Character</code> class for
 * more information).
 * Index values refer to <code>char</code> code units, so a supplementary
 * character uses two positions in a <code>String</code>.
 * <p>The <code>String</code> class provides methods for dealing with
 * Unicode code points (i.e., characters), in addition to those for
 * dealing with Unicode code units (i.e., <code>char</code> values).
 *
 * @author  Lee Boynton
 * @author  Arthur van Hoff
 * @version 1.204, 06/09/06
 * @see     java.lang.Object#toString()
 * @see     java.lang.StringBuffer
 * @see     java.lang.StringBuilder
 * @see     java.nio.charset.Charset
 * @since   JDK1.0
 */

  1、String类代表字符串。作者是Lee Boynton。Java中所有的字符串字面值如“abc”都作为此类的实例实现。

  2、String类的定义中有“final”关键字,字符串是常量,在创建之后不能更改。

  3、Java 语言提供对字符串串联符号("+")以及将其他对象转换为字符串的特殊支持。字符串串联是通过 StringBuilder(或 StringBuffer)类及其 append 方法实现的。

    字符串转换通过Object 类定义的toString 方法实现的,该方法可被 Java 中的所有类继承。

  4、除非另行说明,否则将 null 参数传递给此类中的构造方法或方法将抛出 NullPointerException。  

二、方法

  1、char  charAt(int index):返回字符串指定索引处的字符。 

System.out.println("abc".charAt(1));//结果是:b

  源码:

public char charAt(int index) {
        if ((index < 0) || (index >= count)) {
            throw new StringIndexOutOfBoundsException(index);
        }
        return value[index + offset];
    }
/** The value is used for character storage. char数组用于字符串存储 */
    private final char value[];
public String() {
this.offset = 0;
    this.count = 0;
    this.value = new char[0];
    }

  2、int codePointAt(int index):返回指定索引处的字符的Unicode码值。

System.out.println("abc".codePointAt(0));//结果:97

  3、int codePointBefore(int index):

  4、int codePointCount(int beginIndex,int endIndex)

  5、int compareTo(String anotherString)

  6、int compareToIgnoreCase(String str)

  7、String concat(String str)

  8、boolean contains(CharSequence s)

  9、boolean contentEquence

  copyValueOf

endsWith

equals

equalsIgnoreCase

format

getBytes

hashCode

indexOf

intern

isEmpty

lastIndexOf

length

matches

replace

replaceAll

split

startsWith

toString

valueOf

 

《Just For Java——基础扎实》——第二节:String

标签:

原文地址:http://www.cnblogs.com/JustForJava/p/4676704.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!