闫平平
写代码也要酷酷的!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
统计
 

用代码演示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(): 去掉字符串两端空格

代码如下:

复制代码
 1 package com.aaa.zuoye;
 2 
 3 public class ZuoYe {
 4     public static void main(String[] args) {
 5         fun7();
 6     }
 7 //    (7)String trim(): 去掉字符串两端空格
 8     public static void fun7(){
 9         String str="       helloword    ";
10         str=str.trim();
11         System.out.println(str);
12     }
13 //    (6)String repalce(String old, String newstr): 将字符串中的老字符串,替换为新字符串
14     public static void fun6(){
15          String str="hellohaini";
16          str=str.replaceFirst("h", "1");
17          System.out.println(str);
18     }
19 //    (5)String repalce(char oldChar, char newChar): 将字符串中的老字符,替换为新字符
20     public static void fun5(){
21         String str="hello";
22         str=str.replace("o", "a");
23         System.out.println(str);
24     }
25 //    (4)String toUpperCase(): 字符串转成大写
26     public static void fun4(){
27         String str="hellO";
28         System.out.println(str.toUpperCase());
29     }
30 //    (3)String toLowerCase(): 字符串转成小写
31     public static void fun3(){
32         String str1="helloWord";
33         System.out.println(str1.toLowerCase());
34     }
35 //    (2)char charAt(int index): 返回索引上的字符
36     public static void fun2(){
37         String str1="helloWord";
38         System.out.println(str1.charAt(2));
39     }
40     //(1)boolean isEmpty(): 判断字符串是不是空串,如果是空的就返回true
41     public static boolean fun1(){
42         String str1="";
43         if(str1.isEmpty()==true){
44             return true;
45         }
46         return false;
47     }
48 }
复制代码

 

posted on   写代码也要酷酷的  阅读(230)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
 
点击右上角即可分享
微信分享提示