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毫秒

JAVA练手--String的更多相关文章

  1. 20个Java练手项目,献给嗜学如狂的人

    给大家推荐一条由浅入深的JAVA学习路径,首先完成 Java基础.JDK.JDBC.正则表达式等基础实验,然后进阶到 J2SE 和 SSH 框架学习.最后再通过有趣的练手项目进行巩固. JAVA基础 ...

  2. 去哪找Java练手项目?

    经常有读者在微信上问我: 在学编程的过程中,看了不少书.视频课程,但是看完.听完之后感觉还是不会编程,想找一些项目来练手,但是不知道去哪儿找? 类似的问题,有不少读者问,估计是大部分人的困惑. 练手项 ...

  3. java练手 公约数和公倍数

    Problem D 公约数和公倍数 时间限制:1000 ms  |  内存限制:65535 KB   描述 小明被一个问题给难住了,现在需要你帮帮忙.问题是:给出两个正整数,求出它们的最大公约数和最小 ...

  4. java练手 韩信点兵

    Problem C 韩信点兵 时间限制:3000 ms  |  内存限制:65535 KB   描述 相传韩信才智过人,从不直接清点自己军队的人数,只要让士兵先后以三人一排.五人一排.七人一排地变换队 ...

  5. java 练手 Fibonacci数

    Problem B Fibonacci数 时间限制:3000 ms  |  内存限制:65535 KB   描述 无穷数列1,1,2,3,5,8,13,21,34,55...称为Fibonacci数列 ...

  6. java 练手 谁是最好的Coder

    Problem A 谁是最好的Coder 时间限制:1000 ms  |  内存限制:65535 KB   描述 计科班有很多Coder,帅帅想知道自己是不是综合实力最强的coder. 帅帅喜欢帅,所 ...

  7. 极简易版专家聊天程序--JAVA练手

    呵呵,用JAVA包开发SOCKET连接,是很简单的呢~~~ DailyAdviceServer.java import java.io.*; import java.net.*; public cla ...

  8. JAVA练手--数组

    //数组 public static void main(String[] args) { //1. 数组排序和查找 { int[] intA = {5, 4, 2, 3, 1}; String[] ...

  9. JAVA练手--文件操作

    1. File类 主要作用:用于文件和文件夹的创建.查找.删除等操作 public static void main(String[] args) throws IOException { File ...

随机推荐

  1. Visual Studio Code Angular4 配置环境

    首先在本机安装node.js才能用 npm 命令 环境搭建 安装node.jsnpm install -g @angular/cli 安装第三方库npm install --save bootstra ...

  2. WP8中使用async/await扩展HttpWebRequest

    前文讲到WP8中使用Async执行HTTP请求,用了微软提供的扩展.下面提供了一种方法,自己实现HttpWebRequest的扩展. 随后就可以使用 await HttpWebRequest.GetR ...

  3. how to remote debug in vs 2013

    first download the debugger tools "rtools_setup_x64" start C:\Program Files\Microsoft Visu ...

  4. 知物由学 | AI网络安全实战:生成对抗网络

    本文由  网易云发布. “知物由学”是网易云易盾打造的一个品牌栏目,词语出自汉·王充<论衡·实知>.人,能力有高下之分,学习才知道事物的道理,而后才有智慧,不去求问就不会知道.“知物由学” ...

  5. Android : M 与 N 的权限管理

    从 M 开始,permission 分为 Normal permission Runtime permission Normal permission 直接在 manifest 里声明就可以用了. a ...

  6. linux中使用unzip命令中文乱码解决办法

    今天在使用unzip进行解压缩文件时,发现解压出的文件中文乱码,最后使用如下命令解决: unzip -O CP936 xxx.zip 特此记录一下.

  7. 手动开发PHP模板引擎 一 (35)

    模板叫做TPL,模仿于smarty模板引擎. 我们所说的模板是Web模板,是主要由HTML标记组成的语言来编写的页面,但也有如何表示包含动态生成内容的方式(解析标签).模板引擎是一种软件库,允许我们从 ...

  8. @media媒体查询

    @media媒体查询 @media screen and (min-width:640px) and (max-width:1920px){/*当屏幕尺寸大于640px时与小于1920时*/ .pub ...

  9. httpclient org.apache.http.NoHttpResponseException: host:端口 failed to respond 错误原因和解决方法

    原因:httpclient 之前与服务端建立的链接已经失效(例如:tomcat 默认的keep-alive timeout :20s),再次从连接池拿该失效链接进行请求时,就会保存. 解决方法:官方链 ...

  10. 常见的vue面试题

    001.v-show与v-if的区别v-show:操作的是元素的display属性 v-if:操作的是元素的创建和插入相比较而言v-show的性能要高 002.methods.computed.wat ...