package tet;

public class kk {

    public static void main(String[] args) {
//1.
{
String Stra = "123abc";
//得到指定索引处的char值
char ca = Stra.charAt(3);
System.out.println("1: "+ca);
}
//2. compareTo比较两个对象的差值,compareToIgnoreCase忽略大小写,返回值都为差值
{
String Stra = "abc";
String Strb = "ABC";
String Strc = "aBc";
System.out.println("2: "+Stra.compareTo(Strb));
System.out.println("2: "+Stra.compareToIgnoreCase(Strc));
}
//3. 指定字符串在最后一次出现的位置
{
String Stra = "hello world haha hello";
int i = Stra.lastIndexOf("haha");
System.out.println("3: "+i);
System.out.println("3: "+Stra.lastIndexOf("hello"));
}
//4. 删除字符串中的一个字符
{ //减去hello中的o
String Stra = "hello world haha";
String Strb = Stra.substring(0, 4)+Stra.substring(5);
System.out.println("4: "+Strb);
}
//5. 替换字符串,注意:是返回替换后的,本身不会替换
{
String Stra = "hello world haha";
Stra.replace("hehe", "haha");
System.out.println("5: "+Stra); String Strb = Stra.replace("haha", "hehe");
System.out.println("5: "+Strb);
}
//6. 查找字符串
{
String Stra = "hello world haha";
System.out.println("6: "+Stra.indexOf("world"));
System.out.println("6: "+Stra.indexOf("worle"));
}
//7. 字符串分割
{
String Stra = "www-baidu-com";
String[] suba;
suba = Stra.split("-");
for(String x : suba){
System.out.println("7: "+x);
} String Strb = "xxx hhh ccc"; //注意:ccc前面有两个空格,第一个会检测到,第二个不是的了
String[] subb;
subb = Strb.split(" ");
for(String x : subb){
System.out.println("7: "+x);
}
}
//8. 去掉前后空格
{
String Stra = " hello ";
System.out.println("8: "+Stra+Stra.length());
String Strb = Stra.trim();
System.out.println("8: "+Strb+Strb.length());
}
//9. 大小写转换
{
String Stra = "hello";
String Strb = "HELLO";
String Strc = "HellO";
System.out.println("9: "+Stra.toUpperCase()); //小写转大写
System.out.println("9: "+Strb.toLowerCase()); //大写转小写
System.out.println("9: "+Strc.toUpperCase()); //有大写有小写的字符串也能转换
}
//10. 格式化字符串
{
int i=5;double d = 6.6;
String Stra = String.format("haha=%d,hehe=%f",i,d);
System.out.println("10: "+Stra);
}
//11. 连接字符串
{
String Stra = "hello";
String Strb = Stra.concat(" hehe");
System.out.println("11: "+Stra);
System.out.println("11: "+Strb);
}
//12. 字符串性能测试
{
double current = System.currentTimeMillis();
for(int i=0;i<500000;i++){
String a = "hello";
}
double end = System.currentTimeMillis();
System.out.println("12: 耗时="+(end-current)+"毫秒"); double current1 = System.currentTimeMillis();
for(int i=0;i<500000;i++){
String a = new String("haha");
}
double end1 = System.currentTimeMillis();
System.out.println("12: 耗时="+(end1-current1)+"毫秒");
}
} }

运行结果:

1:  a
2: 32
2: 0
3: 13
3: 18
4: hell world haha
5: hello world haha
5: hello world hehe
6: 6
6: -1
7: www
7: baidu
7: com
7: xxx
7: hhh
7:
7: ccc
8: hello 11
8: hello5
9: HELLO
9: hello
9: HELLO
10: haha=5,hehe=6.600000
11: hello
11: hello hehe
12: 耗时=7.0毫秒
12: 耗时=25.0毫秒

最新文章

  1. gtest学习一:在vs2013中搭建gtest环境
  2. 6.7-3将数组arr中索引值为2的元素替换为“bb”
  3. CSS 控制Html页面高度导致抖动问题的原因
  4. Jsoup Element网页信息采集
  5. postgresql downgrade issue
  6. 安装 ant
  7. Apache启动是出现the requested operation has failed
  8. A Tour of Go Methods continued
  9. 尽量使用ToUpper比较,避免使用ToLower
  10. Android中Gallery显示手机中的图片
  11. 实现简单的跨站脚本攻击(XSS)
  12. Jmeter关联,正则表达式提取器使用
  13. Angular利用@ViewChild在父组件执行子组件的方法
  14. 第一行代码阅读笔记---详解分析第一个Android程序
  15. MyBatis-Spring中间件逻辑分析(怎么把Mapper接口注册到Spring中)
  16. nginx上配置phpmyadmin
  17. 使用@RequestBody将请求体映射到Action方法参数中
  18. python类中的self参数和cls参数
  19. SqlDataReader的用法
  20. Object-C使用类静态方法创建对象时容易内存泄露

热门文章

  1. C#中哈希表(HashTable)的用法详解
  2. C#基础知识入门概要(自我回顾用)
  3. BitAdminCore框架更新日志20180524
  4. Linq的Join == 两个foreach
  5. jQuery--基本介绍与下载
  6. aspx代码审计-1
  7. 【css】—— inline-block 4px 和图片底部 2px bug
  8. Linux之Ubuntu切换root su -
  9. Maven 依赖管理问题小计
  10. jersey annotations