下载此文档

尹其畅 C 教学课件String类.ppt


文档分类:汽车/机械/制造 | 页数:约27页 举报非法文档有奖
1/27
下载提示
  • 1.该资料是网友上传的,本站提供全文预览,预览什么样,下载就什么样。
  • 2.下载该文档所得收入归上传者、原创者。
  • 3.下载的文档,不会出现我们的网址水印。
1/27 下载此文档
文档列表 文档介绍
String类主要用于对字符串内容的检索、比较等操作,操作的结果通常得到一个新字符串,但不会改变源串的内容.
构造方法有如下4个:
public String()
public String(String s)
public String(StringBuffer buf)
public String(char value[ ])
String类
String类
举例:
String s = new String("ABC");
String s = "ABC";
char[] helloArray = { 'h', 'e', 'l', 'l', 'o' };
String helloString = new String(helloArray);
求字符串长度 length()
String s = "Hello!" ;
(());
String类

利用“+”运算符可以实现字符串的拼接,进一步,可以将字符串与任何一个对象或基本数据类型的数据进行拼接。例如:
String s= "Hello!";
s=s+ " Mary "+4; //s的结果为Hello! Mary 4
Java还提供了另一个方法concat(String str)专用于字符串的连接。思考以下程序赋值变化过程:
String s="4+3=";
s=("7"); //新串为4+3=7
String类
public class Example{
String str=new String("good");
char[] ch={'a','b','c'};
public void change(String str,char ch[]){
str="bad";
ch[0]='x';
}
public static void main(String args[]){
Example ex=new Example();
(,);
(+" and "+(new String()));
}
}
【思考】以下程序的输出结果如何?
String类
3. 比较两个字符串
boolean equals(Object anObject)
pareTo(String anotherString)
boolean equalsIgnoreCase(String anotherString)
String类
pareTo(String anotherString)
public class TestString {
public static void main(String a[]) {
String x="hello1";
String y="hello2";
int result = pareTo(y);
(result);
}
}
-1
String类
boolean equals(Object anObject)
String s1="Hello!World";
String s2="Hello!World";
boolean b1=(s2);
boolean b2=(s1==s2);
String类
String s1="Hello!World";
String s2=new String("Hello!World");
boolean b1=(s2);
boolean b2=(s1==s2);
String类
例7-1 设有中英文单词对照表,输入中文单词,显示相应英文单词,输入英文单词显示相应中文单词。
public class Word{
   public static void main(String args[ ]) {
      String [ ][ ] x={ {"good","好"},{"bad","坏"},{"work","工作"}};
      int k;
      String in=args[0];
      if ( (k=find_e(x,in))!=-1)
         (x[k][1]);
      else if ((k=find_c(x,in))!=-1 )
         (x[k][0]);
      else
       

尹其畅 C 教学课件String类 来自淘豆网m.daumloan.com转载请标明出处.

相关文档 更多>>
非法内容举报中心
文档信息
  • 页数27
  • 收藏数0 收藏
  • 顶次数0
  • 上传人mh900965
  • 文件大小207 KB
  • 时间2017-12-31