Java字符串操作

Java String类

字符串长度
字符串连接
字符串截取
字符串比较
字符串搜索
字符串修改
数据转换
字符串大小写
字符串内存分配

//******************************************************************************************//
//*********************String***************************************************************//
//******************************************************************************************//


字符串长度/
int length()
char chars[] = { 'a', 'b', 'c' };
String s = new String(chars);
System.out.println(s.length());   --> 3

字符串连接/
String s = "He is " + age + " years old.";
   --> “He is 9 years old.”
字符串与其他类型数据的连接
int age = 9;
String s = "He is " + age + " years old.";
   --> “He is 9 years old.”
String s = "four: " + 2 + 2;
   --> four: 22
String s = "four: " + (2 + 2);
   --> four:4

字符串截取///

char charAt(int where)
where是想要得到的字符的下标。where的值必须是非负的,它指定了在字符串中的位置。charAt( )方法返回指定位置的字符。例如:
char ch;
ch = "abc".charAt(1);
将“b”赋给ch。

void getChars(int sourceStart, int sourceEnd, char target[ ], int targetStart)
sourceStart指定了子字符串开始的下标,sourceEnd指定了子字符串结束的下一个字符的下标。因此子字符串包含了从 sourceStart到sourceEnd–1的字符。获得字符的数组由target所指定。将被复制子字符串于其中的target的下标由 targetStart指定。注意必须确保的是数组target应该足够大以保证能容纳被指定子字符串中的字符

byte[ ] getBytes( )
getBytes( )方法的其他形式。在将字符串(String)值输出到一个不支持16位Unicode编码的环境时,getBytes( )是最有用的。例如,大多数Internet协议和文本文件格式在文本交换时使用8位ASCII编码

char[ ] toCharArray( )
将字符串(String)对象中的字符转换为一个字符数组

字符串比较///

==是比较对象是否相同
boolean equals(Object str)
这里str是一个用来与调用字符串(String)对象做比较的字符串(String)对象。如果两个字符串具有相同的字符和长度,它返回true,否则返回false。这种比较是区分大小写的。

boolean equalsIgnoreCase(String str)
忽略大小写的比较,str是一个用来与调用字符串(String)对象做比较的字符串(String)对象。如果两个字符串具有相同的字符和长度,它也返回true,否则返回false

boolean regionMatches(int startIndex, String str2,int str2StartIndex, int numChars)
boolean regionMatches(boolean ignoreCase,int startIndex, String str2,int str2StartIndex, int numChars)
startIndex指定了调用字符串(String)对象内区间开始的下标。用于比较的字符串(String)由str2指定的。在str2内,开始比 较区间的下标由str2StartIndex指定。用于比较的子字符串的长度在numChars中。在第二种方案中,如果ignoreCase是 true,字符的大小写被忽略。否则,大小写是有意义的

boolean startsWith(String str)
boolean endsWith(String str)
startsWith( )方法判断一个给定的字符串(String)是否从一个指定的字符串开始。相反地,endsWith( )方法判断所讨论的字符串(String)是否是以一个指定的字符串结尾,如果字符串匹配,返回true。否则返回false

int compareTo(String str)
str是与调用字符串(String)比较的字符串(String)。比较的结果
小于0 调用字符串小于str
大于0 调用字符串大于str
等于0 两个字符串相等

int compareToIgnoreCase(String str)
忽略大小写之外,该方法的返回值与compareTo( )方法相同


字符串搜索///

搜索字符首次出现用
int indexOf(int ch)
搜索字符最后一次出现用
int lastIndexOf(int ch)
这里ch是被查找的字符。

搜索子字符串首次或最后一次出现用
int indexOf(String str)
int lastIndexOf(String str)
这里子字符串由str指定

可以使用如下这些形式指定搜索的起始点:
int indexOf(int ch, int startIndex)
int lastIndexOf(int ch, int startIndex)
int indexOf(String str, int startIndex)
int lastIndexOf(String str, int startIndex)
这里startIndex指定了搜索开始点的下标。对于indexOf( )方法,搜索从startIndex开始到字符串结束。对于lastIndexOf( )方法,搜索从startIndex开始到下标0。下面的例子说明如何利用不同的索引方法在字符串(String)的内部进行搜索:


字符串修改///

String substring(int startIndex)
startIndex指定了子字符串开始的下标。这种形式返回一个从startIndex开始到调用字符串结束的子字符串的拷贝。substring( )方法的第二种形式允许指定子字符串的开始和结束下标

String substring(int startIndex, int endIndex)
startIndex指定开始下标,endIndex指定结束下标。返回的字符串包括从开始下标直到结束下标的所有字符,但不包括结束下标对应的字符

String concat(String str)
连接两个字符串,str的内容跟在调用字符串的后面。concat( )方法与+运算符执行相同的功能

String replace(char original, char replacement)
original指定被由replacement指定的字符所代替的字符,返回得到的字符串
String s = "Hello".replace('l', 'w');
将字符串“Hewwo”赋给s。

String trim( )
删除字符串前面和后面的空白符
String s = " Hello World ".trim();
将字符串“Hello World”赋给s。


数据转换///
static String valueOf(double num)
static String valueOf(long num)
static String valueOf(Object ob)
static String valueOf(char chars[ ])


字符串大小写
String toLowerCase( )
String toUpperCase( )


//******************************************************************************************//
//*********************StringBuffer*********************************************************//
//******************************************************************************************//

StringBuffer( )
StringBuffer(int size)
StringBuffer(String str)
默认构造函数(无参数)预留了16个字符的空间。该空间不需再分配。第二种形式接收一个整数参数,清楚地设置缓冲区的大小。第三种形式接收一个字符串 (String)参数,设置StringBuffer对象的初始内容,同时不进行再分配地多预留了16个字符的空间。当没有指定缓冲区的大小 时,StringBuffer分配了16个附加字符的空间

字符串长度/

int length( )
length( )方法可以得到当前StringBuffer的长度

int capacity( )
capacity( )方法可以得到总的分配容量

字符串内存分配/

void ensureCapacity(int capacity)
分配空间,capacity指定了缓冲区的大小

void setLength(int len)
使用setLength( )方法在StringBuffer对象内设置缓冲区的大小
这里len指定了缓冲区的长度。这个值必须是非负的。当增加缓冲区的大小时,空字符将被加在现存缓冲区的后面。如果用一个小于length( )方法返回的当前值的值调用setLength( )方法,那么在新长度之后存储的字符将被丢失


字符串截取///

char charAt(int where)
使用charAt( )方法可以从StringBuffer中得到单个字符的值。可以通过setCharAt( )方法给StringBuffer中的字符置值。

void setCharAt(int where, char ch)
对于charAt( )方法,where指定获得的字符的下标。对于setCharAt( )方法,where指定被置值的字符的下标,而ch指定了该字符的新值。对于这两种方法,where必须是非负的,同时不能指定在缓冲区之外的位置

void getChars(int sourceStart, int sourceEnd, char target[ ],int targetStart)
sourceStart指定子字符串开始时的下标,而sourceEnd指定了该子字符串结束时下一个字符的下标。这意味着子字符串包含了从 sourceStart到sourceEnd–1位置上的字符。接收字符的数组由target指定。在target内将被复制子字符串的位置下标由 targetStart传递。应注意确保target数组足够大以便能够保存指定的子字符串所包含的字符

字符串连接///

StringBuffer append(String str)
StringBuffer append(int num)
StringBuffer append(Object obj)
将任一其他类型数据的字符串形式连接到调用StringBuffer对象的后面,返回缓冲区本身

StringBuffer insert(int index, String str)
StringBuffer insert(int index, char ch)
StringBuffer insert(int index, Object obj)
将一个字符串插入另一个字符串中

字符串修改///

StringBuffer reverse( )
返回被调用对象的翻转对象

StringBuffer delete(int startIndex, int endIndex)
delete( )方法从调用对象中删除一串字符。这里startIndex指定了需删除的第一个字符的下标,而endIndex指定了需删除的最后一个字符的下一个字符 的下标。因此要删除的子字符串从startIndex到endIndex–1,返回结果的StringBuffer对象

StringBuffer deleteCharAt(int loc)
deleteCharAt( )方法删除由loc指定下标处的字符,返回结果的StringBuffer对象。这里是一个说明delete( )和deleteCharAt( )方法的程序

StringBuffer replace(int startIndex, int endIndex, String str)
被替换的子字符串由下标startIndex和endIndex指定。因此从startIndex到endIndex-1的子字符串被替换。替代字符串在str中传递。返回结果的StringBuffer对象

String substring(int startIndex)
String substring(int startIndex, int endIndex)
第一种形式返回调用StringBuffer对象中从startIndex下标开始直至结束的一个子字符串。第二种形式返回从startIndex开始到endIndex–1结束的子字符串

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值