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) ; } } |
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 } } |
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()
范例:[/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()) ; } } |
相关文章推荐
- 哈尔滨工业大学计算机考研854真题-哈尔滨工业大学 2017年硕士研究生入学考试试题
- 工程硕士专业学位入学考试考试大纲
- 各大计算机公司 笔试及面试 题目 - 阿里巴巴、深信服(Linux的启动流程 V3)
- 各大计算机公司 笔试及面试 题目 - 专题(海量数据处理 二)
- 【转载】强大的计算机工作面试笔试题目...【与中国文学紧密结合】(带非标准答案)
- 浙江大学计算机系硕士研究生复试题目解答(2)
- 04计算机网络基础期中考试题目
- 2011android面试题目及其答案大全
- 各大计算机公司 笔试及面试 题目 - 深信服(八皇后问题)
- 2008年攻读金庸武侠硕士研究生入学统一考试试题
- 2006年全国硕士研究生入学统一考试西医综合科目试题及其答案
- 各大计算机公司 笔试及面试 题目 - 网易(二)
- 各大计算机公司 笔试及面试 题目 - 恒生电子
- 各大计算机公司 笔试及面试 题目 - 人民搜索
- 2007年全国硕士研究生入学统一考试西医综合科目试题及其答案
- 一道计算机二级考试的题目
- 各大计算机公司+笔试及面试+题目-+IGT中国
- 2011 各大IT公司笔试面试题目
- 2011面试题目之猴子偷桃
- 中国科学院大学 2015 年招收攻读硕士学位研究生入学统一考试试题科目名称:计算机学科综合(专业)