String的常用操作方法
来源:互联网 发布:php接口文档 编辑:程序博客网 时间:2024/05/13 13:07
2、String的常用操作方法
2.1、字符与字符串
在String类中提供了以下的方法操作字符与字符串间的转换关系:
|-根据字符串中提供的索引找到指定位置的字符:public char charAt(int index)
|-将字符串变为字符数组:public char[] toCharArray()
|-将字符数组变为字符串:
|-将全部的字符数组变为String类型:public String(char[] value)
|-将部分的字符数组变为String类型:” public String(char[] value,int offset,int count)
范例:取出字符串中制定位置的字符
public class StringAPIDemo01
{
public static void main(String args[])
{
String str = "hello" ;
char c = str.charAt(2) ;
System.out.println(c) ;
}
}
范例:字符串 –-> 字符数组
public class StringAPIDemo02
{
public static void main(String args[])
{
String str = "hello world !!!!!#@" ;
char c[] = str.toCharArray() ;
for (int i = 0 ; i < c.length ; i++ )
{
System.out.print(c[i] + "、" ) ;
}
String str1 = new String(c) ;
String str2 = new String (c,0,5) ;
System.out.println("\n"+str1) ;
System.out.println(str2) ;
}
}
2.2、字节与字符串
与字符数组的操作一致,一个字符串也可以变为字节数组,一个字节数组也可以变为字符串:
|-String à字节数组:public byte[] getBytes()
|-字节数组 àString
|-全部:public String(byte[] bytes)
|-部分:public String(byte[] bytes,int offset,int length)
范例:字节数组 à字符串
public class StringAPIDemo03
{
public static void main(String args[])
{
String str = "hello world !!!!!#@" ;
byte b[] = str.getBytes() ; //将字符串变为字节数组
String str1 = new String(b) ;
String str2 = new String (b,0,5) ;
System.out.println("\n"+str1) ;
System.out.println(str2) ;
}
}
2.3、判断是否以指定的字符串开头或结尾
|-判断是否以指定的字符串开头:public boolean startsWith(String prefix)
|-判断是否以指定的字符串结尾public boolean endsWith(String suffix):
范例:验证操作
public class StringAPIDemo04
{
public static void main(String args[])
{
String str = "**hello world ##" ;
System.out.println(str.startsWith("**")) ;
System.out.println(str.endsWith("##")) ;
}
}
2.4、替换操作
使用以下的方法可以完成替换的操作:
|-public String replaceAll(String regex,String replacement)
范例:替换内容
public class StringAPIDemo05
{
public static void main(String args[])
{
String str = "hello world " ;
String newStr = str.replaceAll("l","x") ;
System.out.println(newStr) ;
}
}
2.5、字符串的截取
使用以下两种方法可以完成字符串的截取操作:
|-全部截取:public String substring(int beginIndex)
|-部分截取:public String substring(int beginIndex,int endIndex)
范例:验证操作
public class StringAPIDemo06
{
public static void main(String args[])
{
String str = "hello world " ;
String sub1 = str.substring(6) ; //从第六个字符开始截取
String sub2 = str.substring(0,5) ; //从第一个字符截取到第五个字符
System.out.println(sub1) ;
System.out.println(sub2) ;
}
}
2.6、字符串的拆分
可以将字符串按照指定的内容进行拆分操作:
|-public String[] split(String regex)
范例:拆分字符串
public class StringAPIDemo07
{
public static void main(String args[])
{
String str = "hello world " ;
String s[] = str.split(" ") ;
for (String st :s)
{
System.out.println(st) ;
}
}
}
2.7、字符串查找
如果需要在一个字符串中查找是否存在指定的内容,可以使用以下的两个方法:
|-取得指定字符串的位置:public int indexOf(int ch),public int indexOf(int ch, int fromIndex)
|-此方法返回int型数据,如果查到了则返回位置,查不到,返回-1 ;
|-直接查找:public boolean contains(String s)
范例:查找操作
public class StringAPIDemo08
{
public static void main(String args[])
{
String str = "hello world " ;
System.out.println(str.contains("hello")) ; //ture
System.out.println(str.contains("aust")) ; //false
}
}
范例:查找位置
public class StringAPIDemo09
{
public static void main(String args[])
{
String str = "hello world " ;
System.out.println(str.indexOf("hello")) ;
System.out.println(str.indexOf("aust")) ;
if((str.indexOf("aust")) != -1)
{
System.out.println("查找到所需的内容。");
}
}
}
范例:指定查找的开始位置
public class StringAPIDemo10
{
public static void main(String args[])
{
String str = "hello world " ;
System.out.println(str.indexOf("hello")) ;
System.out.println(str.indexOf("hello " , 6)) ;
}
}
2.8、字符串的其他操作
去掉左右空格:public String trim()
取的字符串长度:public int length()
转大写:public String toUpperCase()
转小写:public String toLowerCase()
范例:
public class StringAPIDemo11
{
public static void main(String args[])
{
String str = " hello world " ;
System.out.println(str.trim()) ;
System.out.println(str.trim().length());
System.out.println(str.trim().toUpperCase()) ;
}
}
- String的常用操作方法
- String类的常用操作方法
- 笔记:String常用的操作方法
- String类及其常用操作方法
- Android-常用String字符串操作方法
- 常用的文件操作方法
- 线程常用的操作方法
- 线程的常用操作方法
- 常用的文件操作方法
- 文件常用的操作方法
- 多线程的常用操作方法
- 语句 数组 String类常用操作方法
- JAVA常用的日期操作方法
- Java线程常用的操作方法
- js数组常用的操作方法
- 线程--线程常用的操作方法
- String 操作方法
- C#的字符串常用的操作方法
- Matlab修改for循环次数
- shell判断文件,目录是否存在
- javascript继承详解
- Perforce使用中文教程
- 约束(Constraints)
- String的常用操作方法
- JavaScript继承详解
- 小心stringstream使用时的一个陷阱
- C语言“#"和“##”的作用
- 在 Ubuntu 上源码安装 Xen (可以参考)
- StringBuffer 与 StringBuilder
- C#读硬盘序列号的原代码
- Hibernate中Criteria的完整用法
- 飞鸽传书的设计应当具有“完整性”