JAVA语言基础之字符串

C语言中的字符串与字符数组

C语言中没有专门的字符串变量,如果要将一个字符串存放在变量中,必须使用一个字符型数组来存放一个字符串,数组中每一个元素存放一个字符。以'\0'作为结束标志

C语言定义字符串,也就是字符数组的而方式如下

char c[10]={'I', ' ', 'a', 'm', ' ', 'h', 'a', 'p', 'p', 'y'};

JAVA中三种字符串

三种字符串都实现了CharSequence接口


JAVA中的不可变字符串

Java语言使用String类存放不可变字符串

1、构造方法

public    String()    
public    String(String original)
public    String(char[] value)
public    String(char[] value, int offset, int count)
public    String(byte[] bytes)
public    String(byte[] bytes, int offset, int length)
public    String(byte[] ascii, int hibyte)
public    String(byte[] ascii, int hibyte, int offset, int count)
public    String(byte[] bytes, String charsetName)
public    String(byte[] bytes, int offset, int length, String charsetName)
public    String(byte[] bytes, Charset charset)
public    String(byte[] bytes, int offset, int length, Charset charset)
public    String(int[] codePoints, int offset, int count)
public    String(StringBuffer buffer)
public    String(StringBuilder builder)

主要有下面几类:无参构造,传入char数组,byte数组,各种String

2、常用的方法如下:

public char    charAt(int index)   //返回指定位置的字符
public int    codePointAt(int index)  //返回指定位置字符的编码
public int    compareTo(String anotherString)  //说明String实现了排序功能
public int    compareToIgnoreCase(String str)  //忽略大小写排序
public boolean    contains(CharSequence s)     //是否包含指定的字符序列
public boolean    contentEquals(CharSequence cs)  //判断字符串是否和CharSequence相等,后面将比较和Equals的差异
public static String    copyValueOf(char[] data, int offset, int count)
public static String    copyValueOf(char[] data)  //把char数组转成字符串
public boolean    endsWith(String suffix)         // 测试此字符串是否以指定的后缀结束。
public boolean    equals(Object anObject)
public boolean    equalsIgnoreCase(String anotherString)  //判断相等
public static String    format(String format, Object[] args)  //格式化字符串
public static String    format(Locale l, String format, Object[] args)
public byte[] getBytes() //将字符串存储成byte数组
public int    hashCode()   //计算字符串的哈希码
public int    indexOf(int ch)   //计算指定字符串或子字符串在此字符串中第一次出现的位置
public int    length()    //计算字符串长度
public boolean    matches(String regex)  //是否匹配正则表达式
public boolean    startsWith(String prefix, int toffset) 
public boolean    startsWith(String prefix)  //测试此字符串是否以指定的前缀开始
public CharSequence  subSequence(int beginIndex, int endIndex)  //求子串
public String    substring(int beginIndex)
public String    substring(int beginIndex, int endIndex)
public char[]    toCharArray()   //将字符串转换成字符数组
public String    toLowerCase(Locale locale) //大小写转换
public String    toLowerCase()
public String    toUpperCase(Locale locale)
public String    toUpperCase()
public String    trim()   //去掉字符串首尾的空格  
public static String    valueOf(Object obj)  //把任意类型转换成字符串
public static String    valueOf(char[] data)
public static String    valueOf(char[] data, int offset, int count)
public static String    valueOf(boolean b)
public static String    valueOf(char c)
public static String    valueOf(int i)
public static String    valueOf(long l)
public static String    valueOf(float f)
public static String    valueOf(double d)
public void    getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
public boolean    isEmpty() //判断是否为空

String、StringBuffer与StringBuilder的区别

String 和 StringBuffer 的主要区别在于 String 是不可变的对象, 因此在每次对 String 类型进行改变的时候其实都等同于生成了一个新的 String 对象,然后将指针指向新的 String 对象,当内存中无引用String对象多了以后, JVM 的 GC 就会开始工作。所以经常改变内容的字符串最好不要用 String 。当几个个字符串相加时,这种现象尤为明显, 而如果是使用StringBuffer 类则结果就不一样了,每次结果都会对 StringBuffer 对象本身进行操作,而不是生成新的对象,再改变对象引用。
StringBuilder是JDK新增的,和StringBuffer相比,StringBuffer是线程同步的,它的方法都带有Synchronous关键字。所以,二者相比,StringBuilder的效率更高,当不涉及线程安全的时候,我们优先考虑StringBuilder
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值