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. Ubuntu下Jenkins(docker)配置Docker远程启动

    背景: 在做用Jenkins构建docker的操作,需要用Jenkins调用docker命令,需要先安装docker-build-step插件,然后开启docker远程访问. 默认情况下,Docker ...

  2. 【OCP 12c】最新CUUG OCP-071考试题库(61题)

    61.(18-6) choose the best answer: View the Exhibit and examine the structure of the CUSTOMERS table. ...

  3. sql—常用函数

    COUNT()函数 COUNT()函数返回匹配指定条件的行数 SQL COUNT(column_name) 语法 COUNT(column_name) 函数返回指定列的值的数目(NULL 不计入): ...

  4. Writing analyzers

    Writing analyzers There are times when you would like to analyze text in a bespoke fashion, either b ...

  5. Elasticsearch5.4 删除type

    首先要说明的是现在的Elasticsearch已经不支持删除一个type了,所以使用delete命令想要尝试删除一个type的时候会出现如下错误,如果存在一个名为edemo的index和tets的ty ...

  6. Jmeter中一些概念的理解——90%响应时间、事务、并发

    一.90%响应时间(参考虫师博客) 90%Line  一组数由小到大进行排列,找到他的第90%个数(假如是12),那么这个数组中有90%的数将小于等于12 . 用在性能测试的响应时间,也就是90%请求 ...

  7. dbporxy-mysql 协议流转图

    dbproxy 支持 in 查询, 当in 中的字段 属于不同的分表时, QPS约为 5000左右, 如果为 等值查询,  qps的30000左右 主要原因是 对于in操作,会产生多个不同分表的sql ...

  8. 利用Python爬取电影网站

    #!/usr/bin/env python #coding = utf-8 ''' 本爬虫是用来爬取6V电影网站上的电影资源的一个小脚本程序,爬取到的电影链接会通过网页的形式显示出来 ''' impo ...

  9. Linux运维: Rsync同步数据(ubuntu16.04+windows10)

    rsync同步数据 -环境:Linux (ubuntu16.04) + windows10 Linux: 安装 sudo apt-get install rsync rsync --version 查 ...

  10. leetcode-852-山脉数组的峰顶索引

    题目描述: 我们把符合下列属性的数组 A 称作山脉: A.length >= 3 存在 0 < i < A.length - 1 使得A[0] < A[1] < ... ...