JAVA练手--String
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的更多相关文章
- 20个Java练手项目,献给嗜学如狂的人
给大家推荐一条由浅入深的JAVA学习路径,首先完成 Java基础.JDK.JDBC.正则表达式等基础实验,然后进阶到 J2SE 和 SSH 框架学习.最后再通过有趣的练手项目进行巩固. JAVA基础 ...
- 去哪找Java练手项目?
经常有读者在微信上问我: 在学编程的过程中,看了不少书.视频课程,但是看完.听完之后感觉还是不会编程,想找一些项目来练手,但是不知道去哪儿找? 类似的问题,有不少读者问,估计是大部分人的困惑. 练手项 ...
- java练手 公约数和公倍数
Problem D 公约数和公倍数 时间限制:1000 ms | 内存限制:65535 KB 描述 小明被一个问题给难住了,现在需要你帮帮忙.问题是:给出两个正整数,求出它们的最大公约数和最小 ...
- java练手 韩信点兵
Problem C 韩信点兵 时间限制:3000 ms | 内存限制:65535 KB 描述 相传韩信才智过人,从不直接清点自己军队的人数,只要让士兵先后以三人一排.五人一排.七人一排地变换队 ...
- java 练手 Fibonacci数
Problem B Fibonacci数 时间限制:3000 ms | 内存限制:65535 KB 描述 无穷数列1,1,2,3,5,8,13,21,34,55...称为Fibonacci数列 ...
- java 练手 谁是最好的Coder
Problem A 谁是最好的Coder 时间限制:1000 ms | 内存限制:65535 KB 描述 计科班有很多Coder,帅帅想知道自己是不是综合实力最强的coder. 帅帅喜欢帅,所 ...
- 极简易版专家聊天程序--JAVA练手
呵呵,用JAVA包开发SOCKET连接,是很简单的呢~~~ DailyAdviceServer.java import java.io.*; import java.net.*; public cla ...
- JAVA练手--数组
//数组 public static void main(String[] args) { //1. 数组排序和查找 { int[] intA = {5, 4, 2, 3, 1}; String[] ...
- JAVA练手--文件操作
1. File类 主要作用:用于文件和文件夹的创建.查找.删除等操作 public static void main(String[] args) throws IOException { File ...
随机推荐
- [C#学习笔记]分部类和分部方法
知识在于积累. 前言 好久没写博客了,因为在看<CLR via C#>的时候,竟然卡在了分部方法这一小节几天没下去.今天重新认真阅读,有些感悟,所以在此记录. 然后. 每天早晨第一句,&l ...
- iOS 添加字体
1. 将字体(ttf 文件)导入项目. 2. 在项目plist 文件里的 Fonts provided by application 添加新导入的字体. 3. 代码中的调用 [aLabel setFo ...
- 微信小程序如何转换成分与秒
- 学习笔记|JSP教程|菜鸟教程
学习笔记|JSP教程|菜鸟教程 ------------------------------------------------------------------------------------ ...
- python+selenium 定位隐藏元素
定位隐藏要素的原理:页面主要通过“display:none”来控制元素不可见.所以我们需要通过javaScript修改display的值得值为display="block,来实现元素定位的. ...
- Ionic2 自学须知的基本知识点
http://www.cnblogs.com/zsl123/p/5991336.html Ionic(ionicframework)一款接近原生的HTML5移动App开发框架. IONIC 是目前最有 ...
- docker微服务部署之:二、搭建文章微服务项目
docker微服务部署之:一,搭建Eureka微服务项目 一.新增demo_article模块,并编写代码 右键demo_parent->new->Module->Maven,选择M ...
- 3.2 Multi-Master Replication
摘要: 出处:黑洞中的奇点 的博客 http://www.cnblogs.com/kelvin19840813/ 您的支持是对博主最大的鼓励,感谢您的认真阅读.本文版权归作者所有,欢迎转载,但请保留该 ...
- STM32-增量式旋转编码器测量
Development kit:MDK5.14 IDE:UV4 MCU:STM32F103C8T6 一.增量式旋转编码器 1.简介 编码器(encoder)是将信号(如比特流)或数据进行编制.转换为可 ...
- 06-图2 Saving James Bond - Easy Version (25 分)
This time let us consider the situation in the movie "Live and Let Die" in which James Bon ...