java String 常用方法
2024-08-27 17:52:50
String方法
class CeShi{
public static void main(String[] args) {
//toCharArray
char chararraryone[]="qwwdwqfva".toCharArray();//变成数组
print(chararraryone);
System.out.println("\n");
//charAt
//返回所在位置
char a="swfesgegfag".charAt(2);//f
System.out.println(a);
//equalsIgnoreCase
//比较,忽略大小写
System.out.println("qdeqwfqf".equalsIgnoreCase("QDEQWFQF"));//true
//compareTo
// 比较
System.out.println("qe".compareTo("QE"));//32
// lastIndexOf
//查找存在的位置
System.out.println("qqewfsacefa".indexOf("e"));//2
//不存在会返回负一
//contains
//查看是否存在
boolean isb="wjdksalvehello".contains("hello");//true
System.out.println(isb);
//startsWith
boolean isc ="%%%%%lkjicfiesjfv******".startsWith("%%%%%");//true
System.out.println(isc);
//endswith
//同上
//substring
//切片
System.out.println("qwewqrewtewt".substring(1,4));//左开右闭//wew
System.out.println("dsnfgrwuhgiorwgio".substring(1));//从1到最后//snfgrwuhgiorwgio
//split
// 分割
String stingarraryone[]="hello my wwe dcvr".split(" ");
print(stingarraryone);
//trim
//去除两边空格
System.out.println(" wqewq wqfeq wqrewt ".trim());//wqewq wqfeq wqrewt
//toUpperCase,toLowerCase
//全部大小写
System.out.println("dsadwdec".toUpperCase());//DSADWDEC
//replaceAll
//替换
System.out.println("aaasdwasfdewfhello".replace("hello","啥"));//aaasdwasfdewf啥
int [] ari=new int []{1,2,3,4,5,555,432,234,12};
//排序
java.util.Arrays.sort(ari);
print(ari);
int str[]=new int[3];
System.out.println("");
//替换
System.arraycopy(ari,2,str,0,2);
print(str);
}
//以下是定义的方法,用来打印数组。
public static void print(char temp[]){
for(int i=0;i<temp.length;i++){
System.out.print(temp[i]+"\t");
}
}
public static void print(int temp[]){
for (int i=0;i<temp.length;i++){
System.out.print(temp[i]+"\t");
}
}
public static void print(String temp[]){
for(int i=0;i<temp.length;i++){
System.out.print(temp[i]+"\t");
}
System.out.println("\n");
System.out.println("length:\t"+temp.length);
}
public static boolean isNum(char temp[]){//判断数字
for(int i=0;i<temp.length;i++){
if(temp[i]>'9'||temp[i]<'0'){
return false;
}
}
return true;
}
//定义一个方法首字母大写
public static String initcap(String temp){
temp=temp.trim();
temp=temp.substring(0,1).toUpperCase()+temp.substring(1);
return temp;
}
}
- toCharArray
- replaceAll
- toUpperCase,toLowerCase
- trim
- split
- substring
- endswith,startsWith
- contains
- lastIndexOf
- compareTo
- equalsIgnoreCase
- charAt
最新文章
- Java 枚举类的基本使用
- ORA-00060:等待资源时检测到死锁的一种处理方法
- linux基础-第九单元 利用vi编辑器创建和编辑正文文件
- 学习Python的三种境界
- Nginx 负载均衡
- Java Map操作
- Localizing WPF with .resx files
- 异步非阻塞IO的Python Web框架--Tornado
- RIA Service 的 SOAP EndPoint
- 前缀 树 背单词(Remember the Word,LA 3942)
- html css相对单位
- 基本DOS命令之 netstat 命令详解
- Ubuntu Docker 版本的更新与安装
- Chrome浏览器及调试教程
- 在js中网页面写入数据时需要注意的几点
- python 进程锁 生产者消费者模型 队列 (进程其他方法,守护进程,数据共享,进程隔离验证)
- 【windows核心编程】双机调试操作
- 学习笔记之Python Crash Course
- [UE4]哪些数据可以保存
- 在linux 中启动anaconda
热门文章
- 11/5 <;backtracking>; 伪BFS+回溯
- [LeetCode] 96. Unique Binary Search Trees 独一无二的二叉搜索树
- [LeetCode] 19. Remove Nth Node From End of List 移除链表倒数第N个节点
- oracle--AWR,ADDM,ASH区别
- Golang(十二)TLS 相关知识(三)理解并模拟简单代理
- fiddler抓包-1-安装与快速上手
- java登录点击验证码图片切换验证码无效
- sublime-text-3-build-3207 破解+注册码
- Python &#39;\r&#39;, &#39;\n&#39;, &#39;\r\n&#39; 的彻底理解
- JavaIO学习:转换流