String类常用方法。
一,字符数组与字符串。
一个字符串可以变成一个字符数组,同样,一个字符数组可以变成一个字符串。
在String类中提供了以下操作方法。
1)将字符串变成字符数组:public char[] toCharArray();
2) 字符数组变成字符串:public String(char [] value)
public String(char[] value,int offset,int count);//表示从offset位置开始的count个字符,组合成为一个字符串。
public class StringAPIDemo01{
public static void main(String args[]){
String str1 = "hello" ; // 定义字符串
char c[] = str1.toCharArray() ; // 将一个字符串变为字符数组
for(int i=0;i<c.length;i++){ // 循环输出
System.out.print(c[i] + "、") ;
}
System.out.println("") ; // 换行
String str2 = new String(c) ; // 将全部的字符数组变为String
String str3 = new String(c,0,3) ; // 将部分字符数组变为String
System.out.println(str2) ; // 输出字符串
System.out.println(str3) ; // 输出字符串
}
}
运行结果:
h,e,l,l,o
hello
hel
上面分别使用了字符串变数组,和数组变字符串两种方法。
二,从字符串中取出指定位置的字符。
如果要做这种操作,则返回类型肯定是char类型。
使用方法:public char charAt(int index)
public class StringAPIDemo02{
public static void main(String args[]){
String str1 = "hello" ; // 定义String对象
System.out.println(str1.charAt(3)) ; // 取出字符串中第四个字符
}
};
输出结果:
l
三,字符串与bety数组的转换。
byte数组,在一般I/O中经常用到
在String类中,提供以下方法进行字符串和字节数组的转换。
1)字符串变为字节数组:public byte getBytes();
2)讲一个字节数组变为字符串:
将全部数组变为字符串:public String(byte[] byte);
将部分数组变成字符串:public String(char[] byte,int offset,int count);
public class StringAPIDemo03{
public static void main(String args[]){
String str1 = "hello" ; // 定义字符串
byte b[] = str1.getBytes() ; // 将字符串变为byte数组
System.out.println(new String(b)) ; // 将全部的byte数组变为字符串
System.out.println(new String(b,1,3)) ; // 将部分的byte数组变为字符串
}
};
四,取得一个数组的长度。
public int length();
public class StringAPIDemo04{
public static void main(String args[]){
String str1 = "hello LiXingHua" ; // 定义字符串变量
System.out.println("\""+str1+"\"的长度为:"+str1.length()) ;
}
};
五,查找指定字符串是否存在。
在实际操作中,经常会用到判断一个字符串是否存在某些内容,此时就可以使用以下方法:
1)从头开始查找:public int indexof(String str);返回字符串开始的下标。
2)从指定位置查找:public int indexof(String str,int fromIndex);//返回的是一个int数据,表示的是字符串的具体位置,如果没有,返回-1,
public class StringAPIDemo05{
public static void main(String args[]){
String str1 = "abcdefgcgh" ; // 声明字符串
System.out.println(str1.indexOf("c")) ; // 查到返回位置
System.out.println(str1.indexOf("c",3)) ; // 查到返回位置,从第4个位置开始查找
System.out.println(str1.indexOf("x")) ; // 没有查到返回-1
}
};
运行结果:
2,7,-1
六,去掉空格。
trim()可以去掉字符串左右的空格,但是字符串中间的空格是无法去掉的。
public class StringAPIDemo06{
public static void main(String args[]){
String str1 = " hello " ; // 定义字符串
System.out.println(str1.trim()) ; // 去掉左右空格后输出
}
};
运行结果:
hello
七,字符截取。
从一个字符串中取出部分内容,使用的方法是:
1)从指定位置开始截取到最后位置;public String substring(int beginIndex);
2)截取指定范围的位置内容:public String substring(int beginIndex,int endIndex);
public class StringAPIDemo07{
public static void main(String args[]){
String str1 = "hello world" ; // 定义字符串
System.out.println(str1.substring(6)) ; // 从第7个位置开始截取
System.out.println(str1.substring(0,5)) ; // 截取0~5个位置的内容
}
};
运行结果:
world
hello
八,拆分字符串。
如果现在需要按指定字符串去拆分一个字符串的话,则使用:public String[] split(String regex)
public class StringAPIDemo08{
public static void main(String args[]){
String str1 = "hello world" ; // 定义字符串
String s[] = str1.split(" ") ; // 按空格进行字符串的拆分
for(int i=0;i<s.length;i++){ // 循环输出
System.out.println(s[i]) ;
}
}
运行结果:
hello
world
例子按照空格,被拆分成两个字符串,形成一个包含两个字符串的字符串数组。
九,大小写转换。
将一个大写的字符串全部变成小写:public String toUpperCase();
讲一个小写的字符串全部变成大学:public String toLowerCase();
public class StringAPIDemo09{
public static void main(String args[]){
System.out.println("将\"hello world\"转成大写:" + "hello world".toUpperCase()) ;
System.out.println("将\"HELLO WORLD\"转成小写:" + "HELLO WORLD".toLowerCase()) ;
}
运行结果:
HELLO WORLD
hello word
十,判断是否以指定的字符串开头或者结尾。
在String中可以以以下方法完成。
1)是否以指定字符串开头:public boolean starsWith(string prefix)
2)是否以指定字符串结尾:public boolean endWith(string suffix)
public class StringAPIDemo10{
public static void main(String args[]){
String str1 = "**HELLO" ; // 定义字符串
String str2 = "HELLO**" ; // 定义字符串
if(str1.startsWith("**")){ // 判断是否以“**”开头
System.out.println("(**HELLO)以**开头") ;
}
if(str2.endsWith("**")){ // 判断是否以“**”结尾
System.out.println("(HELLO**)以**结尾") ;
}
}
};
运行结果:
(**HELLO)以**开头
(HELLO**)以**结尾
十一,不区分大小写的字符串比较。
public boolean equalsIgnoreCase(String anotherString)
public class StringAPIDemo11{
public static void main(String args[]){
String str1 = "HELLO" ; // 定义字符串
String str2 = "hello" ; // 定义字符串
System.out.println("\"HELLO\" equals \"hello\" " + str1.equals(str2)) ;
System.out.println("\"HELLO\" equalsIgnoreCase \"hello\" "
+ str1.equalsIgnoreCase(str2)) ; // 不区分大小写的比较
}
};
运行结果:
false
ture
十二,字符串替换。
使用这个方法完成字符串替换:
public String replaceAll(String regex,String replacement);
public class StringAPIDemo12{
public static void main(String args[]){
String str = "hello" ; // 定义字符串
String newStr = str.replaceAll("l","x") ; // 现在将所有的l替换成x
System.out.println("替换之后的结果:" + newStr) ;
}
};
运行结果:
替换之后的结果:hexxo
String类常用方法。的更多相关文章
- JAVA中String类常用方法 I
String类常用方法有: int length() -– 返回当前字符串的长度 int indexOf(int ch) -– 查找ch字符在该字符串中第一次出现的位置 int indexOf(Str ...
- JAVA String类常用方法
一.String类String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象.java把String类声明的final类,不能有类.String类对象创建 ...
- 菜鸡的Java笔记 第十四 String 类常用方法
/*String 类常用方法 将所有String类的常用方法全部记下来,包括方法名称,参数作用以及类型 一个成熟的编程语言,除了它的语法非常完善之外,那么也需要提供有大量的开发类库 ...
- Java 中的 String 类常用方法
字符串广泛应用在Java编程中,在Java中字符串属于对象,String 类提供了许多用来处理字符串的方法,例如,获取字符串长度.对字符串进行截取.将字符串转换为大写或小写.字符串分割等. Strin ...
- Java的String类常用方法
一.构造函数 String(byte[ ] bytes):通过byte数组构造字符串对象. String(char[ ] value):通过char数组构造字符串对象. String(Sting or ...
- String类常用方法
1.String类的特点,字符串一旦被初始化就不会被改变. 2.String对象定义的两种方式 ①String s = "affdf";这种定义方式是在字符串常量池中创建一个Str ...
- Java中String类常用方法(字符串中的子字符串的个数)
重点内容 4种方法: 1.int indexOf(String str)返回第一次出现的指定子字符串在此字符串中的索引. 2.int indexOf(String str, int startInde ...
- String类常用方法练习
String 类代表字符串.Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现. 字符串是常量:它们的值在创建之后不能更改.字符串缓冲区支持可变的字符串. ...
- java的异常抛出和String类常用方法
一.异常抛出 异常是程序的异种非错误的意外情况,分为运行期异常(RuntimeException)和编译期异常(CheckedExcption) 处理异常可以用try——catch或自定义 impor ...
随机推荐
- 个人总结 HTML+CSS
从大一下学期接触,一直到今年,接触的时间也挺长的了,最近一些认识的盆友和同学说是想学习前端,自己也开始慢慢停下脚步,不再拼命地去学很多框架的东西,回归到基础,慢慢把基础打牢 很多知识碎片一直来不及整理 ...
- css复习笔记
margin: 1. 当有三个值时第一个值为上,第二个值为左右,第三个值为下. 2.margin外边距折叠,水平没有,垂直会折叠,且折叠后以两者最大值为准.另外,当一个元素包裹另一元素时也会发生折叠. ...
- Sass学习之路(2)——Sass环境安装(windows版)
因为本喵目前用的是window10的本子,所以这里就发windows版本的安装流程啦.(希望有朋友可以赞助我一个mac(┳_┳)): 第一步:安装ruby 因为Sass是基于ruby编写的,所以先去官 ...
- 屏幕字段结构SCREEN的字段含义
在SAP屏幕中,一个字段就像.NET中的一个控件,这个字段对应一个SCREEN结构,就像控件的多个属性. SE11可以查看SCREEN结构中的字段,只是没有备注. 名称 长度 说明 NAME 屏幕字段 ...
- Microsoft Dynamics CRM 2011/2013 JS操作集锦
1.Xrm.Page.context用户ID:getUserId()用户角色:getUserRoles()用户语言:getUserLcid()组织名称:getOrgUniqueName()组织语言:g ...
- SharePoint 2013 - User
1. 在SharePoint 2010中,可以搜索出NT AUTHORITY\authenticated users,但在SharePoint 2013中,不能搜索出,需要手动写入全名后进行验证: 2 ...
- SharePoint 2013 App Remote Event Receivers
当我们在使用App的时候,就会发现一些问题,比如那些网站部署.更新或者卸载了,我们很关心我们的App是否有人用,这就需要远程事件接收器了. 1.在我们的测试App的解决方案上选中鼠标,按F4弹出属性, ...
- linux中fork()函数详解(原创!!实例讲解) (转载)
一.fork入门知识 一个进程,包括代码.数据和分配给进程的资源.fork()函数通过系统调用创建一个与原来进程几乎完全相同的进程, 也就是两个进程可以做完全相同的事,但如果初始参数或者传入的变量不 ...
- SVN版本更新后,upData工程之后,Xcode 工程文件打不开解决办法
svn更新代码后,打开xcode工程文件,会出现 xxx..xcodeproj cannot be opened becausethe project file cannot be parsed. ...
- android媒体文件扫描
项目中可能有这样的需求:下载或导入.导出的图片.音乐等媒体文件,需要马上能在图库或本地视屏播放器中显示出来,或者要能在媒体数据库中查询到媒体文件的相关信息,这时我们就得主动通知系统扫描新的媒体文件了. ...