您的位置:首页 > 理论基础

2011杭州电子科技大学计算机硕士入学考试面试题目

2011-04-07 12:27 369 查看

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)
范例:[/b]取出字符串中制定位置的字符
public class StringAPIDemo01
{
public static void main(String args[])
{
String str = "hello" ;
char c = str.charAt(2) ;
System.out.println(c) ;
}
}
范例:[/b]字符串 –-> 字符数组
public class StringAPIDemo02
{
public static void main(String args[])
{
String str = "hello world !!!!!#@" ;
char c[] = str.toCharArray() ;[/b]
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)
范例:[/b]字节数组 à字符串
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):
范例:[/b]验证操作
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)

范例:[/b]替换内容
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)
范例:[/b]验证操作
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)
范例:[/b]拆分字符串
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)
范例:[/b]查找操作
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
}
}
范例:[/b]查找位置
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("查找到所需的内容。");
}
}
}
范例:[/b]指定查找的开始位置
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()
范例:[/b]
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()) ;
}
}
本文出自 “笑问客” 博客,请务必保留此出处http://javalinux.blog.51cto.com/1027174/362720
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: