Java用代码演示String类中的以下方法的用法
2024-10-16 03:22:41
用代码演示String类中的以下方法的用法
(1)boolean isEmpty(): 判断字符串是不是空串,如果是空的就返回true
(2)char charAt(int index): 返回索引上的字符
(3)String toLowerCase(): 字符串转成小写
(4)String toUpperCase(): 字符串转成大写
(5)String repalce(char oldChar, char newChar): 将字符串中的老字符,替换为新字符
(6)String repalce(String old, String newstr): 将字符串中的老字符串,替换为新字符串
(7)String trim(): 去掉字符串两端空格
代码如下:
package com.aaa.zuoye; public class ZuoYe {
public static void main(String[] args) {
fun7();
}
// (7)String trim(): 去掉字符串两端空格
public static void fun7(){
String str=" helloword ";
str=str.trim();
System.out.println(str);
}
// (6)String repalce(String old, String newstr): 将字符串中的老字符串,替换为新字符串
public static void fun6(){
String str="hellohaini";
str=str.replaceFirst("h", "1");
System.out.println(str);
}
// (5)String repalce(char oldChar, char newChar): 将字符串中的老字符,替换为新字符
public static void fun5(){
String str="hello";
str=str.replace("o", "a");
System.out.println(str);
}
// (4)String toUpperCase(): 字符串转成大写
public static void fun4(){
String str="hellO";
System.out.println(str.toUpperCase());
}
// (3)String toLowerCase(): 字符串转成小写
public static void fun3(){
String str1="helloWord";
System.out.println(str1.toLowerCase());
}
// (2)char charAt(int index): 返回索引上的字符
public static void fun2(){
String str1="helloWord";
System.out.println(str1.charAt(2));
}
//(1)boolean isEmpty(): 判断字符串是不是空串,如果是空的就返回true
public static boolean fun1(){
String str1="";
if(str1.isEmpty()==true){
return true;
}
return false;
}
}
最新文章
- thoughtworks编程题
- Java新集合示意图
- u3d单词学习plane
- Java ArrayListSerialise
- docker stop 与 docker kill的区别
- discuz 门户栏目URL跳转异常的问题
- hihoCoder挑战赛14 A,B,C题解
- Android判断应用程序从后台回到前台
- Python challenge 3 - urllib &;amp; re
- [转]解决VS2008 开发Windows Mobile 项目生成速度慢的问题
- NOIP2014-普及组复赛-第二题-比例简化
- devexpress表格控件gridcontrol图片列,按钮列,时间列等特殊列的实现
- Rails 定时任务——whenever实现周期性任务
- jmeter导入DB数据再优化
- 在 Ubuntu14.04 上搭建 Spark 2.3.1(latest version)
- spring 整合junit进行测试
- Hadoop简单介绍
- 玩转spring boot——war部署
- QT开发之旅四邮件发送工具
- CEF之CefSettings设置日志等级