51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 2062|回复: 1
打印 上一主题 下一主题

[讨论] java-测试开发字符串

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2018-3-6 16:08:07 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. package j2se;

  2. public class StringDemo {
  3.     private String demoString="ceshixiaoyouning";
  4.      
  5.     public void testString(){
  6.         String dsg=demoString.concat("very beautiful");//字符串相加,也可以用+来表示
  7.         System.out.println(dsg);
  8.          
  9.         int len=demoString.length();//字符串的长度
  10.         System.out.println(len);
  11.          
  12.         boolean eq="ceshixiaoyouning".equals(demoString);//比较两个字符串是否相等
  13.         System.out.println(eq);
  14.          
  15.         String sub=demoString.substring(5, 8);//取子字符串,从第5个字符开始,到底8个字符,但是不包含第8个字符
  16.         System.out.println(sub);
  17.          
  18.         String subString=demoString.substring(5);//取子字符串,从第5个字符开始一直到字符串尾
  19.         System.out.println(subString);
  20.          
  21.         boolean sw =demoString.startsWith("ceshi");//判断是否以某个字符串开头
  22.         System.out.println(sw);
  23.          
  24.         boolean ew=demoString.endsWith("youning");//判断石头以某个字符串结尾
  25.         System.out.println(ew);
  26.          
  27.         int subIndex=demoString.indexOf("ce");//找出子字符串在字符串中第一次出现的index,如果找不到则返回-1
  28.         System.out.println(subIndex);
  29.          
  30.         int lastIndex=demoString.lastIndexOf("i");//找出子字符串在字符串最后一次出现的index,如果找不到则返回-1
  31.         System.out.println(lastIndex);
  32.          
  33.         System.out.println(demoString.toUpperCase());//字符串中的字全变成大写
  34.         System.out.println(demoString.toLowerCase());//字符串中的字全变成小写
  35.         System.out.println("    youning   ".trim());//将字符串首尾的空格去掉
  36.          
  37.         String subReplace=demoString.replace("ceshi", "hello");//将字符串中的某段字符串替换成新的字符串
  38.         System.out.println(subReplace);
  39.          
  40.         String subReplaceF=demoString.replaceFirst("i", "hhh");//将字符串中第一次出现的子字符串替换成新的字符串,支持正则
  41.         System.out.println(subReplaceF);
  42.          
  43.         String subReplaceA=demoString.replaceAll("i", "hhh");//将字符串中出现的所有子字符串替换成新的子字符串,支持正则
  44.         System.out.println(subReplaceA);
  45.     }
  46.      
  47.     public static void main(String[] args) {
  48.         StringDemo s=new StringDemo();
  49.         s.testString();
  50.     }
  51. }
复制代码
运行结果:

ceshixiaoyouningvery beautiful
16
true
xia
xiaoyouning
true
true
0
13
CESHIXIAOYOUNING
ceshixiaoyouning
youning
helloxiaoyouning
ceshhhhxiaoyouning
ceshhhhxhhhaoyounhhhng

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-9-20 21:27 , Processed in 0.061674 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表