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 ...
随机推荐
- jQuery Mobile笔记
1.获取jQuery mobile 文件,访问jQuerymobile网站下载 (貌似使用jquery mobile后,jquery会自动在网页中添加一些class类,第一次知道的我是被吓呆的!!) ...
- [转] X-RIME: 基于Hadoop的开源大规模社交网络分析工具
转自http://www.dataguru.cn/forum.php?mod=viewthread&tid=286174 随着互联网的快速发展,涌现出了一大批以Facebook,Twitter ...
- 使用tinypng优化Android的资源图片
tinypng 是一个支持压缩png和jpg图片格式的网站,通过其独特的算法(通过一种叫“量化”的技术,把原本png文件的24位真彩色压缩为8位的索引演示,是一 种矢量压缩方法,把颜色值用数值123等 ...
- IOS 杂笔-10(Base64 加密)
base64加密是可逆的,因此并不是很安全,在一些注重安全的地方很少用到,但是在普通传输中可以使用. 切忌,base64加密是不安全的. // // ViewController.m // CX-Ba ...
- iOS 学习 - 8 TableViewCell 自适应高度
思路:计算文字的高度,存进数组 加注:存在中文,需要加一行文字的高度,也就是 font 主要代码 #pragma mark -- UITableViewDelegate - (CGFloat)tabl ...
- [android] 手机卫士保存密码时进行md5加密
一般的手机没有root权限,进不去data/data目录,当手机刷机了后,拥有root权限,就可以进入data/data目录,查看我们保存的密码文件,因此我们需要对存入的密码进行MD5加密 获取Mes ...
- 干货-iOS、mac开源项目及库,以后我也会持续更新。
昨晚在网上看的干货,直接分享给大家了,觉得有用的,直接fork吧. https://github.com/Brances/TimLiu-iOS
- ReactiveCocoa中信号的使用
前言: 很早之前就有看过ReactiveCocoa,那会看的时候知道是一个新的框架关于响应式编程,具体什么也没有深入研究,今天也对ReactiveCocoa这个框架的使用进行了一定的了解.在githu ...
- 【等待事件】序列等待事件总结(enq: SQ - contention、row cache lock、DFS lock handle和enq: SV - contention)
[等待事件]序列等待事件总结(enq: SQ - contention.row cache lock.DFS lock handle和enq: SV - contention) 1 BLOG文档结 ...
- Servlet生命周期+工作原理
Servlet生命周期+工作原理 1.Servlet的生命周期: Servlet加载,加载,服务,销毁. 2.典型函数解释: Init():这个函数是用来初始化Servlet对象的.在 ...