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 ...
随机推荐
- Angular4 配置问题
出现错误: Local workspace file ('angular.json') could not be found.Error: Local workspace file ('angular ...
- rpm包的安装,查询,卸载,升级,校验,数据库重建,验证数据包
rpm命名: 包:组成部分 主包:bind-9.7.1-1.i586.e15.rpm 子包:bind-lib-9.7.1-1.i586.e15.rpm bind-utils-9.7.1-1.i586. ...
- 在jquery中怎么使用css类名和id来获取元素?
在jquery中,你可以很容易的使用CSS类名和id类获取元素. 例如: 1.ID:#id $('#idA')——选择id为idA的所有元素,不管元素的标签名如何. $('div#idA')——选择i ...
- File Path Directory总结
阅读目录 开始 Path 对路径 字符串进行操作 获得后缀 能合并路径 获取文件名 Directory和DirectoryInfo 对目录进行操作 判断目录是否存在 创建目录 删除目录 获取目录下所 ...
- [UWP开发]在windows10中设置壁纸~UserProfilePersonalizationSettings
在之前的wp8.1和wp8中,微软没有公开设置壁纸的API,只有一个设置锁屏的API,但在Windows10中,微软为我们提供了设置壁纸的API:TrySetWallpaperImageAsync,他 ...
- Python(模块&包)
参考:https://www.cnblogs.com/yuanchenqi/articles/5732581.html 在linux下给pycharm安装第三方库,需要在.bashrc中加: 因为对应 ...
- robot framework学习笔记之十-模板
测试模板可以让关键字驱动测试用例转换为数据驱动测试用例.鉴于普通测试用例是由关键字和可能的参 数组成,使用了模板的测试用例只需要定义模板关键字的参数即可
- delphi 10.2 ---treeview 基本用法
unit Unit2; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...
- SpringCloud学习笔记(一)——基础
什么是微服务架构 简单地说,微服务是系统架构上的一种设计风格,它的主旨是将一个原本独立的系统拆分成多个小型服务,这些小型服务都在各自独立的进程中运行,服务之间通过基于HTTP的RESTful API进 ...
- mongodb 两台互为主从
主机A [root@mysql_master zhxf]# cat docker-compose.yml version: '3' services: mongo_rs1: image: mongo: ...