




已阅读5页,还剩25页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第14章 文本和数字的使用 14.1 string 类 字符串函数 14.2 数字操作 数学函数(在第3章讲过) 14.3 产生随机数(了解) 14.1 string类 - 字符串函数 n字符串比较:compareto、equals n去空格字符:trim、trimstart、trimend n加空格字符:padleft、padright n字符串长度:length n字符串查找:chars、indexof、 lastindexof n替换字符串:replace n大小写转换:tolower、toupper n获取子串:substring n删除子串:remove n插入子串:insert n判断开头结尾:startswith、endswith n一般方法:dim s,r as string s=”abcd” 结果变量=s. 字符串函数(参数) 结果变量的类型由字符串函数返回类型决定。 n字符串比较开关: option compare binary/text nbinary:大小写不同 ntext:大小写相同 n默认binary。 n设置方法:直接写语句,或有关菜单对话框。 右击工程|属性|通用属性|生成,作用于整个工 程。 1、chars(i) 功能:返回字符串中的第i个字符 。 ni从0开始。 n使用方法:dim s as string dim c as char s=”abcd” c=s.chars(1) c的值为b 2、length 功能:返回字符串中的字符个数。 n使用方法:dim s as string dim len as integer s=”abcd” len=s.length len的值为4 3、compareto(t) 功能:当前字符串与字符串t比较 。 n返回值为-1(比t小)、0 (与t等) 、1 (比t大) 。 n是否区分大小写按option compare binary/text 定。默认区分大小写。 n使用方法:dim s,r as string dim result as integer s=”abcd” r=”abdd” result=s.compareto(r) result的值为-1,ss result=s.compareto(”abcd”) result的值为0,s=”abcd” 4、satrtswith(t) 功能:判断当前字符串是否以字 符串t开头。永远区分大小写。 n返回值为false、true。 n如果t为空,则产生异常argumentnullexception n使用方法:dim s as string dim result as boolean s=”abcd” result=s.startswith(”ac”) result的值为false result=s.startswith(”ab”) result的值为true 5、endswith(t) 功能:判断当前字符串是否以字符 串t结尾。永远区分大小写 n返回值为false、true。 n如果t为空,则产生异常argumentnullexception n使用方法:dim s as string dim result as boolean s=”abcd” result=s.endswith(”ac”) result的值为false result=s.endswith(”cd”) result的值为true 6、equals(t) 功能:判断当前字符串是否与字符串t 相等。永远区分大小写 n返回值为false、true。 n如果t为空,则产生异常argumentnullexception n如果t非字符串,则产生异常argumentexception n使用方法:dim s as string dim result as boolean s=”abcd” result=s.equals(”abcd”) result的值为false result=s.equals(”abcd”) result的值为true 6、indexof(t) 功能:判断字符串t在当前字 符串中首次出现的位置。永远区分大小写 n返回值为整数。-1表示未发现。 n使用方法:dim s as string dim result as integer s=”abcd” result=s.indexof(”bc”) result的值为1 result=s. indexof(”x”) result的值为-1 6、indexof(t,i) 功能:判断字符串t在当前 字符串中第i个字符开始首次出现的位置。永 远区分大小写 n返回值为整数。-1表示未发现。 n使用方法:dim s as string dim result as integer s=”abcbcd” result=s.indexof(”bc”,2) result的值为3 result=s. indexof(”abc”,1) result的值为-1 6、indexof(t,i,len) 功能:判断字符串t在当前 字符串中第i个字符开始首次出现的位置,只比较当 前字符串中len个字符。永远区分大小写 n返回值为整数。-1表示未发现。 n使用方法:dim s as string dim result as integer s=”abcbcd” result=s.indexof(”bc”,2,2) result的值为-1 result=s. indexof(”abc”,0,6) result的值为0 7、lastindexof(t) 功能:判断字符串t在当 前字符串中最后一次出现的位置。永远区分大 小写 n返回值为整数。-1表示未发现。 n使用方法:dim s as string dim result as integer s=”abcbcd” result=s.lastindexof(”bc”) result的值为3 result=s.lastindexof(”x”) result的值为-1 7、lastindexof(t,i) 功能:判断字符串t在 当前字符串中第i个字符开始最后一次出现的 位置。永远区分大小写 n返回值为整数。-1表示未发现。 n使用方法:dim s as string dim result as integer s=”abcbcd” result=s.lastindexof(”bc”,2) result的值为3 result=s. lastindexof(”abc”,0) result的值为-1| 7、lastindexof(t,i,len) 功能:判断字符串t在 当前字符串中第i个字符开始最后一次出现的位置, 只比较当前字符串中len个字符。永远区分大小写。 n返回值为整数。-1表示未发现。 n使用方法:dim s as string dim result as integer s=”abcbcd” result=s.lastindexof(”bc”,2,2) result的值为-1 result=s. lastindexof(”abc”,1,5) result的值为-1 8、insert(i,t) 功能:将字符串t插入在当前 字符串中第i个字符的位置。 n返回值为插入后的字符串。原字符串不变。 n使用方法:dim s,r as string s=”abcd” r=s.insert(”xyz”,1) r的值为”axyzbcd”。 s不变。 9、padleft(len) 功能:将若干个空白字符插 入在当前字符串开头,使得字符串长度为len 。如果len比原来串长度小,则什么也不加。 n返回值为插入后的字符串。原字符串不变。 n使用方法:dim s,r as string s=”abcd” r=s.padleft(8) r的值为” abcd”, 前面加4个空格。 r=s.padleft(2) r的值为”abcd”。s不变。 9、padleft(len,c) 功能:将若干个字符c插 入在当前字符串开头,使得字符串长度为len 。如果len比原来串长度小,则什么也不加。 n返回值为插入后的字符串。原字符串不变。 n使用方法:dim s,r as string s=”abcd” r=s.padleft(8,”x”) r的值为”xxxxabcd”, 前面加4个x。 r=s.padleft(2,”x”) r的值为”abcd”。 s不变。 10、padright(len) 功能:将若干个空白字 符插入在当前字符串末尾,使得字符串长度为 len。如果len比原来串长度小,则什么也不加 。 n返回值为插入后的字符串。原字符串不变。 n使用方法:dim s,r as string s=”abcd” r=s.padright(8) r的值为”abcd ”, 后面加4个空格。 r=s.padright(2) r的值为”abcd”。 s不变。 10、padright(len,c) 功能:将若干个字符 c插入在当前字符串末尾,使得字符串长度为 len。如果len比原来串长度小,则什么也不加 。 n返回值为插入后的字符串。原字符串不变。 n使用方法:dim s,r as string s=”abcd” r=s.padleft(8,”x”) r的值为”abcdxxxx”, 后面加4个x。 r=s.padleft(2,”x”) r的值为”abcd”。 s不变。 11、remove(i,len) 功能:将当前字符串 中第i个字符开始的len个字符删除。 n返回值为删除后的字符串。原字符串不变。 n使用方法:dim s,r as string s=”abcd” r=s.remove(1,2) r的值为”ad”。 s不变。 r=s.remove(4,5) 产生异常argumentoutofrange 11、remove(i,len) 异常处理: n使用try/catch/end try语句: dim s,r as string s=”abcd” try r=s.remove(4,5) catch e as argumentoutofrangeexception msgbox(”something is wrong.”) msgbox(e.message & e.source) end try 12、replace(t1,t2) 功能:将当前字符串 中t1字符串用t2字符串代替。 n返回值为替换后的字符串。原字符串不变。 n使用方法:dim s,r as string s=”abcdbc” r=s.replace(”bc”,”xyz”) r的值为”axyzdxyz”。 s不变。 13、substring(i,len) 功能:将当前字符串 中第i个字符开始的长度为len的子串返回。 n原字符串不变。 n使用方法:dim s,r as string s=”abcdbc” r=s.substring(2,3) r的值为”cdb”。 s不变。 14、toupper() 功能:将当前字符串中字符 全部变成大写后返回。 n原字符串不变。 n使用方法:dim s,r as string s=”abcdbc” r=s.toupper() r的值为”abcdbc”。 s不变。 15、tolower() 功能:将当前字符串中字符 全部变成小写后返回。 n原字符串不变。 n使用方法:dim s,r as string s=”abcdxy” r=s.tolower() r的值为”abcdxy”。 s不变。 16、trim() 功能:将当前字符串首尾的空格 字符全部删除后返回。 n原字符串不变。 n使用方法:dim s,r as string s=” abcdxy ” r=s.trim() r的值为”abcdxy”。 s不变。 17、trimstart() 功能:将当前字符串首的空 格字符全部删除后返回。 n原字符串不变。 n使用方法:dim s,r as string s=” abcdxy ” r=s.trimstart() r的值为”abcdxy
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 读我要做个好孩子有感350字13篇
- 老师谢谢您250字10篇
- 纪念白求恩余映潮课件
- 小说人物塑造手法探究教学教案
- 2025年审计师考试审计职业道德与法律责任试卷
- 2025年期货从业资格考试衍生品市场与交易试卷
- 公司股权转让详细协议签署事项
- 清丰县期中卷子数学试卷
- 全国一卷江西数学试卷
- 内蒙古三模数学试卷
- GB/T 18051-2000潜油电泵振动试验方法
- 广告投放“冷启动期”及“ocpm起量”的底层逻辑
- 小学音乐《村晚》优质课件设计
- 竞选团支书幽默大气简短六篇
- 知名投资机构和投资人联系方式汇总
- (完整word版)教育部发布《3-6岁儿童学习与发展指南》(全文)
- 混凝土监理旁站记录
- 部门会签单模板
- G12《贷款质量迁徙情况表》填报说明
- 县城市管理领域集中行使行政处罚权工作衔接规范(试行)
- 结肠息肉的护理查房精编ppt
评论
0/150
提交评论