11字符串.ppt

上传人:hyn****60 文档编号:71551046 上传时间:2023-02-03 格式:PPT 页数:17 大小:420KB
返回 下载 相关 举报
11字符串.ppt_第1页
第1页 / 共17页
11字符串.ppt_第2页
第2页 / 共17页
点击查看更多>>
资源描述

《11字符串.ppt》由会员分享,可在线阅读,更多相关《11字符串.ppt(17页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、1字符串2字符串o在Java中,有一个内建的类String,叫字符串。其实例就是字符串对象。字符串对象是用双引号括起来的字符序列,但字符串的内容并不包括双引号。o字符串是有序的字符序列,它的最基本元素是字符(字母、数字、标点符号等)。o字符串是对象,不是基本类型的数据。字符串变量是引用类型的变量。字符串对象的创建o直接使用字符串常量n如:”Hello”o使用String的构造方法实例化字符串nString()/实例化一个空字符串。nString(charvalue)/使用字符数组构造一个字符串.nString(charvalue,intoffset,intcount)/使用字符数组的一部分构造

2、一个字符串.nString(Stringoriginal)/从一个字符串新建一个字符串(复制字符串)nString(StringBufferbuffer)/从一个字符串缓冲对象构造一个字符串n等等3重载的“+”运算符4可以用可以用“+”运算符连接两个字符串。运算符连接两个字符串。可以用可以用“+”运算符让一个字符串与一个数值量进行连接运算符让一个字符串与一个数值量进行连接(会自动将数值量转化成字符串)。在连接时,当两个以上(会自动将数值量转化成字符串)。在连接时,当两个以上的数值量要进行算术运算时,可以用()括起来,否则按字的数值量要进行算术运算时,可以用()括起来,否则按字符串进行处理。符串

3、进行处理。String s1=new String(“aaa”);String s2=new String(“bbb”);s1=s1+s2;System.out.println(“s1+s2=”+s1);输出结果为输出结果为s1+s2=aaabbbString s1,s2;s1=+12+12;s2=+(12+12);s3=12+12+“System.out.println(s1);System.out.println(s2);System.out.println(s3);输出结果为输出结果为:1212 24 24字符串的常用方法方法方法用途用途length()返回字符串的长度,其类型是intc

4、oncat(Strings)返回字符串与s字符串连接形成的新串charAt(int index)返回字符串中下标index所对应的字符(下标从0开始)getChars(int srcBegin,int srcEnd,char dst,int dstBegin)将字符串中的部分字符存入字符数组中toCharArray()根据字符串的内容返回一个字符数组chars=ab.toCharArray();substring(int beginIndex)substring(int beginIndex,int endIndex)返回字符串从下标beginIndex到下标endIndex-1之间的子串。5

5、例题:以下程序的运行结果是什么?packagemypackage;publicclassTestSubstringpublicstaticvoidmain(Stringargs)Strings=abcd;System.out.println(s.substring(1,3);6运行结果:bc字符串的常用方法方法用途replace(charoldChar,charnewChar)replace(StringoldStr,StringnewStr)将字符串的字符oldChar替换成newChar并且返回toLowerCase()大写字母转换为小写并返回toUpperCase()小写字母转换为大写并

6、返回trim()删除String串中的首尾空格并返回indexOf(Stringstr)indexOf(Stringstr,intfromIndex)indexOf(charc)indexOf(charc,intfromIndex)检索字符串中是否含有子串str。若有则返回子串出现位置的下标;若没有则返回-1。检索顺序从下标fromIndex开始向后搜索。lastIndexOf(Stringstr)lastIndexOf(Stringstr,intfromIndex)lastIndexOf(charc)lastIndexOf(charc,intfromIndex)作用和上面相同,但是是反向搜索

7、7例题:下面程序的输出结果是?package mypackage;public class Testreplacepublic static void main(String args)String s=abcd;System.out.println(s.replace(b,h);System.out.println(s.replace(bc,h);8运行结果:ahcdahd字符串的常用方法方法用途equals(Stringstr)equalsIgnoreCase(Stringstr)将字符串的内容与str的内容比较,若一致(大小写也一致),则返回true,否则返回false。后者比较时忽略大

8、小写。compareTo(Stringstr)按字典顺序比较字符串与str的大小。若相等返回0,大于str返回正数,小于str则返回负数regionMatches(boolean ignoreCase,int offset,String str1,int offset1,int len)将字符串从下标offset开始、长度为len的子串与字符串str1从offset1开始、长度为len的字串做相等的比较,若相等则返回true,否则返回falsestartsWidth(Stringstr)endsWidth(Stringstr)判断字符串是否以str为前缀(后缀)9例题:下面程序的运行结果是?p

9、ackagemypackage;publicclassTestregionMatchespublicstaticvoidmain(Stringargs)Strings=abcd;System.out.println(s.regionMatches(true,0,Bd,0,1);System.out.println(s.regionMatches(true,1,Bc,0,1);System.out.println(s.regionMatches(true,1,Bd,0,2);System.out.println(s.regionMatches(true,0,Bc,0,2);System.out.

10、println(s.regionMatches(true,1,Bc,0,2);10falsetruefalsefalsetrue字符串的常用方法方法用途静态方法valueOf(一个参数)参数可以是各种类型返回一个表示参数的字符串11例题:下面程序运行的结果是?classStudentStringname=张山“;publicclassTestValueOfpublicstaticvoidmain(Stringargs)doubles=123.45;booleanb=true;charc=w,e,a,r,e,o,k;Strings1=String.valueOf(s);Studentstuden

11、t=newStudent();Strings2=String.valueOf(student);System.out.println(s1);System.out.println(String.valueOf(c);System.out.println(String.valueOf(b);System.out.println(s2);12123.45weareoktruemypackage.Student150bd4d练习与例题:字符串排序o编写一个类编写一个类SortString,类中有一个方法,类中有一个方法sortStringArray(),(),该方法以一个字符串数组为参数,返回按字典

12、该方法以一个字符串数组为参数,返回按字典顺序排好队的数组。编写一个顺序排好队的数组。编写一个main方法,测试上述排方法,测试上述排序方法。序方法。o如:数组如:数组“Now”,“is”,“the”,“all”排好序后应该排好序后应该为为“Now”,“all”,“is”,“the”o提示:字符串的比较可以使用字符串的提示:字符串的比较可以使用字符串的compareTo()方法()方法13参考答案public static class SortStringpublic String sortStringArray(String sa)for(int i=0;isa.length;i+)for(i

13、nt j=i+1;j0)String temp=sai;sai=saj;saj=temp;return sa;14参考答案public static void main(String args)String strArray=You,are,my,good,friend;strArray=SortString.sortStringArray(strArray);for(int i=0;istrArray.length;i+)System.out.println(strArrayi);15字符串缓冲类StringBuffero适合对字符串做连接或修改等操作而不会建立新的对象。速度更快,更省资源。

14、o常用方法nStringBufferappend(charc)nStringBufferappend(Stringstr)nStringBufferdeleteCharAt(intindex)nStringBufferinsert(intk,charc)nStringBufferinsert(intk,Stringstr)nStringBufferreplace(intm,intn,Stringstr)nStringBufferreverse()16StringBuffer应用示例public class TestStringBuffer public static void main(String args)StringBuffer sb=new StringBuffer(world);sb.append(!);sb.insert(0,Hello);String s=new String(sb);System.out.println(s);sb=new StringBuffer(abcdefghijklmnopqrstuvwxyz);sb.reverse();System.out.println(sb);17

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 生活常识

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com