Java中的字符串

        从概念上讲,Java中的字符串是Unicode字符的序列。Java没有内置的字符串类型,而是定义了String类,用来创建和操作字符串。每个带引号的字符串都是java.lang.String类的一个实例。需要注意:Strings are Immutable。字符串一旦生成,是不可变的。

        public final class String extends Object implements Serializable, Comparable<String>,CharSequence

构造函数:

public String():创建一个String对象,代表空字符串。由于String的不可变,这个构造函数没有意义。

public String(String original):创建一个字符串的拷贝。由于String的不可变,这个构造函数没有意义。


public String(int[] codePoints,int offset, int count):从codePoints中截取一定长度的子数组构造字符串对象。


char[] 数组转为字符串,可以设定起始位置,长度。

public String(char[] value):

public String(char[] value,int offset, int count):


byte[] 数组转为字符串,可以设定起始位置,长度,以及字符集。没有指定字符集时,默认选择平台默认编码格式的字符串对象。

public String(byte[] bytes,int offset, int length, String charsetName):

public String(byte[] bytes, String charsetName):

public String(byte[] bytes,int offset, int length, Charset charset):

public String(byte[] bytes, Charset charset):

public String(byte[] bytes,,int offset, int length):

public String(byte[] bytes):


public String(StringBuffer buffer);

public String(StringBuilder builder):

静态函数:

static String valueOf(boolean b);

static String valueOf(char c); static String valueOf(char[] data);static String valueOf(char[] data,in offset, int count);

static String valueOf(double d);

static String valueOf(float f);

static String valueOf(int i);

static String valueOf(long l);

static String valueOf(Object obj);

static String copyValueOf(char[] data);

static String copyValueOf(char[] data,int offset, int count);

static String format(loacal l,String format,Object...args);

static String format(String format,Object...args);

常用函数:

char charAt(int index)

int compareTo(String anotherString)

int compareToIgnoreCase(String anotherString)

String concat(String str)

booolean contains(CharSequence s)

boolean contentEquals(CharSequence cs)

boolean contentEquals(StringBuffer sb)

boolean startsWith(String prefix):是否以prefix开始。

boolean startsWith(String prefix,int toffset):toffset位置,是否以prefix开始。

boolean endWith(String suffix):是否以suffix结束。

byte[] getBytes(String charsetName)

byte[] getBytes()

int indexOf(int ch)                         int lastIndexOf(int ch)        

int indexOf(String str)                    int lastIndexOf(String str)

boolean isEmpty()

boolean matches(String regex)

String substring(int bgeinIndex) String substring(int bgeinIndex,int endIndex)

String replace(char oldChar, char newChar)

String比较

       使用s.equals(t)或者s.equalsIgnoreCase(t)来比较。不能使用==。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容