JavaSE(05)(String类)

****String类总结:**
统计一个字符串中大写小写字母和数字的个数:
package zz.itheima.string;

import java.util.Scanner;

public class CalcNum {


        //示例:统计一个字符串中大写小写字母和数字的个数
        public static void main(String[] args) {
            System.out.println("请任意输入一个字符串:");
            Scanner sc=new Scanner(System.in);
            String val=sc.nextLine();

            int n1=0;
            int n2=0;
            int n3=0;

            //1.先转换为byte类型的数组
            byte[] temp=val.getBytes();
            //2.循环遍历,判断范围,计数
            for (int i = 0; i < temp.length; i++) {
                byte t=temp[i];
                //48-57是数字   65-90是大写字母   97-122是小写字母
                if(t>=48&&t<=57)
                    n1++;
                if(t>=65&&t<=90)
                    n2++;
                if(t>=97&&t<=122)
                    n3++;
            }
            System.out.println("数字有"+n1+",大写字符有"+n2+"小写字符有"+n3);
    }

}
字符串的分割:
package zz.itheima.string;

import java.util.Scanner;

public class TestCaseCast {
    //字符串的分割
    public static void main(String[] args) {
        //练习:把任意字符串的首字母转成大写,其余为小写
        System.out.println("请任意输入一个字符串:");
        Scanner sc=new Scanner(System.in);
        String val=sc.nextLine();

        String first=val.substring(0, 1);
        first=first.toUpperCase();

        String last=val.substring(1);
        last=last.toLowerCase();

        System.out.println(first+last);
    }

}
字符串的获取:
package zz.itheima.string;

import java.util.Scanner;

public class TestHuoqu {

    public static void main(String[] args) {
//      int length()
        String s1="hlelhlo";
        System.out.println(s1.length());//7

//      char charAt(int index)
        System.out.println(s1.charAt(3));//l

//      int indexOf(int ch) 和int indexOf(String str); 找不到返回-1
        System.out.println(s1.indexOf("hl"));//0

//      int indexOf(int ch,int fromIndex)和int indexOf(String str,int fromIndex)
        System.out.println(s1.indexOf("hl",3));//4,从第三个位置开始首次出现的位置

//      int lastIndexOf(String s)
        System.out.println("last"+s1.lastIndexOf("hl"));//last4

//      int lastIndexOf(String s,int fromIndex)
        System.out.println("last"+s1.lastIndexOf("hl",3));//last0

//      String substring(int start)
        System.out.println(s1.substring(2));  //包含开始下标的字符

//      String substring(int start,int end)
        System.out.println(s1.substring(2, 4));   //不包含结束下标的字符

        //示例:判断电子邮件的格式是否正确  zhangsan@qq.com
        System.out.println("请输入你的邮箱地址:");
        Scanner sc=new Scanner(System.in);
        String email=sc.nextLine();

        //必须有@ 和 .   @必须在.之前
        if(email.indexOf("@")!=-1&&email.indexOf("@")<email.indexOf("."))
            System.out.println("正确");
        else 
            System.out.println("不正确");  

        //练习:换行输出字符串中的每一个字符
        for (int i = 0; i < email.length(); i++) {
            char temp=email.charAt(i);
            System.out.println(temp);
        }
    }

}
7
l
0
4
last4
last0
elhlo
el
请输入你的邮箱地址:
lqc@qq.com
正确
字符串的判断:
package zz.itheima.string;

public class TestJudge {

    public static void main(String[] args) {
        String s1 = "hello";
        String s2 = "hEllo";
        //boolean equals(Object obj)
        System.out.println(s1.equals(s2));//false
        //boolean equalsIgnoreCase(String str)
        System.out.println(s1.equalsIgnoreCase(s2));//true
        //boolean contains(String str)
        System.out.println(s1.contains("he"));//true
        //boolean startsWith(String str)
        System.out.println(s1.startsWith("h"));//true

        //boolean endsWith(String str)
        System.out.println(s1.endsWith("llo"));//true
        //boolean isEmpty()     “”  null
        String s3=null;//没有开辟堆内存
        //s3.length();

        String s4="";
        System.out.println(s4.isEmpty());
        System.out.println(s4.length());//0

    }

}
运行结果:
false
true
true
true
true
true
0
模拟用户登录:
package zz.itheima.string;

import java.util.Scanner;

public class TestLogin {

    public static void main(String[] args) {
    //示例:模拟管理员登录(账号和密码),给三次机会,并提示还有几次(假设正确的账号和密码是: admin  0000)

        Scanner sc=new Scanner(System.in);
        for(int j=1;j<4;j++)
        {
            //1.给出提示信息(账号)
            System.out.println("请输入账号:");
            String account=sc.nextLine();
            //2.给出提示信息(密码)
            System.out.println("请输入密码:");
            String pwd=sc.nextLine();
            //3.判断
            if(account.equalsIgnoreCase("admin")&&pwd.equals("abcd"))
            {
                System.out.println("登录成功");
                break;
            }
            else {
                System.out.println("登录失败,你还剩"+(3-j)+"次机会");
            }

        }
    }

}
String测试类:
package zz.itheima.string;

public class TestString {
    //String测试类
    public static void main(String[] args) {
        //1.String类是一个特殊的引用类型,可以像基本数据类型一样使用
        int j = 10;
        String s1 = "hello";
        //2。String类的构造方法
        //空构造public String()
        String s2 = new String();//已经开辟了堆内存
        System.out.println("s2="+s2);
        //参数为字节数组 public String(byte[] bytes)
        byte[] bytes = {97,98,99};
        String s3 = new String(bytes);
        System.out.println("s3="+s3);
        //参数为字符数组 public String(char[] values)
        char[] chars = {'a','b','c'};
        String s4 = new String (chars);
        System.out.println("s4="+s4);
        //String对象的不可变性,也就是一旦初始化后值不可以改变
        String s5 = new String("hello");
        s5 += ",world";//重新指向了一个新的地址,原来的地址(存放hello)还存在,只是没有引用了,只能被垃圾回收掉了。
        System.out.println("s5="+s5);
        System.out.println("**********************");
        System.out.println("abc"=="ab"+"c");
        String s6 = "abc";
        String s7 = "ab";
        String s8 = "c";
        System.out.println(s6==s7+s8);
    }

}
运行结果:
s2=
s3=abc
s4=abc
s5=hello,world
**********************
true
false
字符串的转换:
package zz.itheima.string;

public class TestZhuanhuan {

    public static void main(String[] args) {
        //字符串转换测试
//      byte[] getBytes()
        String s1="hello";
        byte[] b=s1.getBytes();
        for (int i = 0; i < b.length; i++) {
            System.out.println(b[i]);
        }
//      char[] toCharArray()
        char[] c=s1.toCharArray();
        for (int i = 0; i < c.length; i++) {
            System.out.println(c[i]);
        }
//      static String valueOf(char[] chs)把字符数组转化为字符串
        String s2=String.valueOf(c);
        System.out.println(s2);

//      static String valueOf(基本数据类型 i)   类似于“+”,但是要比“+”效率要高,把基本数据类型转化为字符串
        System.out.println(String.valueOf(10.0));
        //+作用是连接字符串
        System.out.println(s2+"asdf");
        //+还能起到类型转换的作用,其他类型会转换为字符串的
        int n=10;
        System.out.println(n+"");

//      String toLowerCase()
        String s3="heLLo";
        System.out.println(s3.toLowerCase());

//      String toUpperCase()
        System.out.println(s3.toUpperCase());

//      String concat(String str)       类似于 “+”
        s3=s3.concat(",world");
        System.out.println(s3);
    }
}
运行结果:
104
101
108
108
111
h
e
l
l
o
hello
10.0
helloasdf
10
hello
HELLO
heLLo,world
其他的一些常用方法:
package zz.itheima.string;

public class TestOther {

    public static void main(String[] args) {
//      替换功能:
//      String replace(char old,char new)
        String s1="abcdecfg";
        s1=s1.replace('c', 'x');
        System.out.println(s1);

//      String replace(String old,String new)
        s1="abcdecfg";
        s1=s1.replace("de", "xyz");
        System.out.println(s1);

//      分割  String[] split(String regex)
        String s2="abc-defg-hijk";
        String[] ss=s2.split("-");
        for (int i = 0; i < ss.length; i++) {
            System.out.println(ss[i]);
        }

        String s3="-abc-defg";
        String[] st=s3.split("-");
        System.out.println(st.length);

//      去除字符串两端的空格  String trim()
        String s4="    hello   world   ";
        System.out.println(s4.trim());

//      练习1:把字符串中的“o”替换成“0”,“l”替换成“1”
        String s5="hellolosdflool";
        s5=s5.replace("o", "0").replace("l", "1");
        System.out.println(s5);

//      练习2:给一个字符串"5 , 4 , 3 ",然后分别换行输出
        String s6="1,2,3,4,5";
        String[] temp=s6.split(",");
        for (int i = 0; i < temp.length; i++) {
            System.out.println(temp[i]);
        }
    }

}
运行结果:
abxdexfg
abcxyzcfg
abc
defg
hijk
3
hello   world
he11010sdf1001
1
2
3
4
5


























  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!
提供的源码资源涵盖了小程序应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值