字符串函数java_java字符串常用函数及其用法(最新整理)

《java字符串常用函数及其用法(最新整理)》由会员分享,可在线阅读,更多相关《java字符串常用函数及其用法(最新整理)(7页珍藏版)》请在人人文库网上搜索。

1、java 中的字符串也是一连串的字符。但是与许多其他的计算机语言将字符串作为字符数组处理不同,Java 将字符串作为 String 类型对象来处理。将字符串作为内置的对象处理允许 Java 提供十分丰富的功能特性以方便处理字符串。下面是一些使用频率比较高的函数及其相关说明。String 相关函数1)substring()它有两种形式,第一种是:String substring(int startIndex) 第二种是:String substring(int startIndex,int endIndex) 2)concat() 连接两个字符串例 :String s=Welcome to ; 。

2、String t=s.concat(AnHui);3)replace() 替换它有两种形式,第一种形式用一个字符在调用字符串中所有出现某个字符的地方进行替换,形式如下:String replace(char original,char replacement)例如:String s=”Hello”.replace(l,w);第二种形式是用一个字符序列替换另一个字符序列,形式如下: String replace(CharSequence original,CharSequence replacement) 4)trim() 去掉起始和结尾的空格5) valueOf() 转换为字符串6) toLo。

3、werCase() 转换为小写7)toUpperCase() 转换为大写8)length() 取得字符串的长度例 :char chars=a,b.c; String s=new String(chars); int len=s.length();9)charAt() 截取一个字符例:char ch;ch=”abc”.charAt(1);返 回 值 为 b 10)getChars() 截取多个字符void getChars(int sourceStart,int sourceEnd,char target,int targetStart) sourceStart 指定了子串开始字符的下标sour。

4、ceEnd 指定了子串结束后的下一个字符的下标。因此,子串包含从sourceStart 到 sourceEnd-1 的字符。target 指定接收字符的数组targetStart target 中开始复制子串的下标值例:String s=”this is a demo of the getChars method.”; char buf=new char20;s.getChars(10,14,buf,0); 11)getBytes()替代 getChars()的一种方法是将字符存储在字节数组中,该方法即 getBytes()例:String s = “Hello!你好!”;Java 字符串函数。

5、java 中的字符串也是一连串的字符。但是与许多其他的计算机语言将字符串作为字符数组处理不同,Java 将字符串作为 String 类型对象来处理。将字符串作为内置的对象处理允许Java 提供十分丰富的功能特性以方便处理字符串。下面是一些使用频率比较高的函数及其相关说明。String 相关函数1)substring()它有两种形式,第一种是:String substring(int startIndex) 第二种是:String substring(int startIndex,int endIndex) 2)concat() 连接两个字符串例 :String s=Welcome to ; S。

6、tring t=s.concat(AnHui);3)replace() 替换它有两种形式,第一种形式用一个字符在调用字符串中所有出现某个字符的地方进行替换, 形式如下:String replace(char original,char replacement)例如:String s=”Hello”.replace(l,w);第二种形式是用一个字符序列替换另一个字符序列,形式如下: String replace(CharSequence original,CharSequence replacement) 4)trim() 去掉起始和结尾的空格5) valueOf() 转换为字符串6) toLo。

7、werCase() 转换为小写7)toUpperCase() 转换为大写8)length() 取得字符串的长度例:char chars=a,b.c; String s=new String(chars); int len=s.length(); 9)charAt() 截取一个字符例:char ch;ch=”abc”.charAt(1);返 回 值 为 b 10)getChars() 截取多个字符void getChars(int sourceStart,int sourceEnd,char target,int targetStart) sourceStart 指定了子串开始字符的下标sour。

8、ceEnd 指定了子串结束后的下一个字符的下标。因此,子串包含从 sourceStart 到sourceEnd-1 的字符。target 指定接收字符的数组targetStart target 中开始复制子串的下标值例:String s=”this is a demo of the getChars method.”; char buf=new char20;s.getChars(10,14,buf,0);11)getBytes()替代 getChars()的一种方法是将字符存储在字节数组中,该方法即 getBytes()例:String s = “Hello!你好!”; byte bytes。

9、 = s.getBytes(); 12)toCharArray()例:String s = “Hello!你好!”; char ss = s.toCharArray();13) equals()和 equalsIgnoreCase() 比较两个字符串14) regionMatches() 用于比较一个字符串中特定区域与另一特定区域,它有一个重载的形式允许在比较中忽略大小写。boolean regionMatches(int startIndex,String str2,int str2StartIndex,int numChars) boolean regionMatches(boolean 。

10、ignoreCase,int startIndex,Stringstr2,int str2StartIndex,int numChars) 15)startsWith()和 endsWith()startsWith()方法决定是否以特定字符串开始endWith()方法决定是否以特定字符串结束16)equals()和=equals()方法比较字符串对象中的字符=运算符比较两个对象是否引用同一实例。例:String s1=”Hello”;String s2=new String(s1); s1.eauals(s2); /true s1=s2;/false17) compareTo()和 compa。

11、reToIgnoreCase() 比较字符串18) indexOf()和 lastIndexOf()indexOf() 查找字符或者子串第一次出现的地方。lastIndexOf() 查找字符或者子串是后一次出现的地方。19) trim 去空格函数例: String t1 = abcde;System.out.println(t1.trim();/ 去掉开头和结尾的空格“abc de” 20)split 字符串分割String y = abc,de,fg,hi,jk;String y1 = y.split(,);/ 截取字符串所有,字符for (int i = 0; i y1.length; i。

12、+) System.out.print(y1i);/ 输出结果 abcdefghijk21)append 添加或插入函数StringBuffer zz1 = new StringBuffer(z1);/ append 插入字符zz1.append(|).append(hijk).append(/).append(lmn).append(opq); System.out.println();System.out.print(zz1);/ 输出:abcdefg|hijk/lmnopqStringBuffer 构造函数StringBuffer 定义了三个构造函数:StringBuffer() Str。

13、ingBuffer(int size)StringBuffer(String str) StringBuffer(CharSequence chars) 下面是 StringBuffer 相关的函数: 1)length()和 capacity()一个 StringBuffer 当前长度可通过 length()方法得到,而整个可分配空间通过 capacity()方法得到。2)ensureCapacity() 设置缓冲区的大小void ensureCapacity(int capacity) 3)setLength() 设置缓冲区的长度void setLength(int len) 4)charA。

14、t()和 setCharAt() char charAt(int where)void setCharAt(int where,char ch) 5)getChars()void getChars(int sourceStart,int sourceEnd,char target,int targetStart)6) append() 可把任何类型数据的字符串表示连接到调用的 StringBuffer 对象的末尾。例:int a=42;StringBuffer sb=new StringBuffer(40);String s=sb.append(”a=”).append(a).append(”。

15、!”).toString(); 6)insert() 插入字符串StringBuffer insert(int index,String str) StringBuffer insert(int index,char ch) StringBuffer insert(int index,Object obj)7) index 指定将字符串插入到 StringBuffer 对象中的位置的下标。8) reverse() 颠倒 StringBuffer 对象中的字符StringBuffer reverse()9) delete()和 deleteCharAt() 删除字符StringBuffer de。

16、lete(int startIndex,int endIndex) StringBuffer deleteCharAt(int loc)10) replace() 替换StringBuffer replace(int startIndex,int endIndex,String str) 11)substring() 截取子串String substring(int startIndex)String substring(int startIndex,int endIndex)“”“”At the end, Xiao Bian gives you a passage. Minand once 。

17、said, people who learn to learn are very happy people. In every wonderful life, learning is an eternal theme. As a professional clerical and teaching position, I understand the importance of continuous learning, life is diligent, nothing can be gained, only continuous learning can achieve better sel。

18、f. Only by constantly learning and mastering the latest relevant knowledge, can employees from all walks of life keep up with the pace of enterprise development and innovate to meet the needs of the market. This document is also edited by my studio professionals, there may be errors in the document, if there are errors, please correct, thank you。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值