字符串的简单应用

  1. public class join {
  2. public static void main (String args[]){
  3. String s1 = new String ("hello");
  4. String s2 = new String ("word");
  5. String s = s1 + s2;
  6. int size= s.length();
  7. System.out.println(size);
  8. }
  9. }//输出字符串长度
  10.  
  11. /*public class join {
  12. public static void main (String args[]){
  13. String s1 = new String ("hello");
  14. String s2 = new String ("word");
  15. String s = s1 + " " + s2;
  16. System.out.println(s);
  17. }
  18. }//连接多个字符串*/
  19. /*public class join {
  20. public static void main (String args[]){
  21. String s1 = new String ("hello");
  22. String s2 = new String ("word");
  23. String s = s1 + s2;
  24. int size= s.indexOf("e"); //在这里可以修改局部变量 "e" 可以改变成其他的浮点
  25. System.out.println(size);
  26. }
  27. }//字符串的查找
  28. */
  29. /*public class join {
  30. public static void main (String args[]){
  31. String s1 = new String ("hello");
  32. String s2 = new String ("word");
  33. String s = s1 + s2;
  34. int size= s.lastIndexOf(""); //""中间没有空格表示空字符串
  35. System.out.println("空字符在字符串中的索引位置"+size);
  36. System.out.println("字符串的长度是" + s.length());
  37. }
  38. }//查找空字符串的位置并输出长度*/

连接其他数据类型(Link型

  1. public class Link {
  2. public static void main(String args[]) {
  3. int booktime = 4;
  4. float practicetime = 2.5f;
  5. System.out.println( "我每天花费 " + booktime + "小时看书;" + practicetime + "小时练习");
  6. }
  7. }

索引位置x的对应位置的字母是什么

  1. public class join {
  2. public static void main (String args[]){
  3. String s1 = new String ("hello");
  4. String s2 = new String ("word");
  5. String s = s1 + " " + s2;
  6. char j =s.charAt(6);
  7. System.out.println("索引位置6所对的字母是"+j);
  8. }
  9. }

 获取字符串

  1. public class join {
  2. public static void main (String args[]){
  3. String s1 = new String ("hello");
  4. String s2 = new String ("word");
  5. String s = s1+s2;
  6. System.out.println(s);
  7. String y=s.substring(0, 3);
  8. System.out.println(y);
  9. }
  10. }

去除空格

  1. public class join {
  2. public static void main (String args[]){
  3. String s1 = new String ("hello");
  4. String s2 = new String ("word");
  5. String s = " "+s1+" "+s2+" ";
  6. System.out.println("原来的字符串长度为"+s.length());
  7. System.out.println("去掉空格后的字符串长度为"+s.trim().length());
  8. }
  9. }

 字符串的替换

  1. public class join {
  2. public static void main (String args[]){
  3. String s1 = new String ("hello");
  4. String s2 = new String ("word");
  5. String s = " "+s1+" "+s2+" ";
  6. System.out.println(s);
  7. System.out.println("替换之后的"+s.replace("word", "java"));
  8. }
  9. }

替换之后是hello  java

判断字符串的开始和结尾

  1. public class startorend {
  2. public static void main(String args[]) {
  3. String num1="12345678";
  4. String num2="23456789";
  5. boolean b=num1.startsWith("12");
  6. boolean c=num1.endsWith("89");
  7. boolean d=num2.startsWith("12");
  8. boolean e=num2.endsWith("89");
  9. System.out.println("num1输出的是否以12开始"+b);
  10. System.out.println("num1输出的是否以89结束"+c);
  11. System.out.println("num2输出的是否以12开始"+d);
  12. System.out.println("num1输出的是否以89结束"+e);
  13. }
  14. public startorend() {
  15. // TODO 自动生成的构造函数存根
  16. }
  17.  
  18. }

用equal 和equalignore判断字符串是否相等

  1. public class Opinion {
  2. public static void main(String args[]) {
  3. String a=new String("abc");
  4. String b=new String ("ABC");
  5. String c=new String ("abc");
  6. boolean x=a.equals(b);
  7. boolean y=a.equalsIgnoreCase(b);
  8. System.out.println(x);
  9. System.out.println(y);
  10. }
  11. public Opinion() {
  12. // TODO 自动生成的构造函数存根
  13. }
  14.  
  15. }

按照字典顺序比较两个字符串

  1. public class Opinion{
  2. public static void main(String args[]) {
  3. String s1="a";
  4. String s2="b";
  5. String s3="c";
  6. System.out.println(s1 +" "+"comepareTo"+" "+s2+s1.compareTo(s2));
  7. System.out.println(s1+" "+"comepareTo"+" "+s3+s1.compareTo(s3));
  8.  
  9. }
  10. }

字符串字母大小写转换

  1. public class Opinion{
  2. public static void main(String args[]) {
  3. String s1="abc";
  4. String s2="LZB";
  5. String s=s1+s2;
  6. String news1=s.toLowerCase();
  7. String news2=s.toUpperCase();
  8. System.out.println(s);
  9. System.out.println(news1+news2);
  10. }
  11. }

日期和时间字符串格式化 

  1. import java.util.Date;
  2. @SuppressWarnings("unused")
  3. public class date {
  4. public static void main(String[] args) {
  5. Date days=new Date();//创建DATE的类day
  6. String year =String.format("%tY", days);
  7. String month=String.format("%tB", days);
  8. String day=String.format("%td", days);
  9. System.out.println("今年是"+year+"年");
  10. System.out.println("现在是"+month);
  11. System.out.println("今天是"+day+"号");
  12. }
  13. public date() {
  14. // TODO 自动生成的构造函数存根
  15. }
  16.  
  17. }

时间格式化

  1. import java.util.Date;
  2. public class date {
  3. public static void main(String[] args) {
  4. Date time=new Date();//创建DATE的类time
  5. String hour =String.format("%tH", time);
  6. String minute=String.format("%tM", time);
  7. String second=String.format("%tS", time);
  8. System.out.println("现在的时间是"+hour+"时"+minute+"分"+second+"秒");
  9. }
  10. public date() {
  11. // TODO 自动生成的构造函数存根
  12. }
  13.  
  14. }
  1. import java.util.Date;
  2. public class date {
  3. public static void main(String[] args) {
  4. Date days=new Date();//创建DATE的类time
  5. String day =String.format("%tc", days);
  6. String form=String.format("%tF", days);
  7. System.out.println("全部的时间信息是"+day);
  8. System.out.println("年-月-日格式"+form);
  9. }
  10. public date() {
  11. // TODO 自动生成的构造函数存根
  12. }
  13.  
  14. }
  1. 实验结果:
  2. 全部的时间信息是星期二 二月 04 09:54:45 CST 2020
  3. 年-月-日格式2020-02-04
  4.  
  5. 常规类型格式化

  1. public class Opinion{
  2. public static void main(String args[]) {
  3. String s1=String.format("%d", 400/2);
  4. String s2=String.format("%b", 2>7);
  5. String s=String.format("%x", 100);
  6. System.out.println("2>7"+s2);
  7. System.out.println("400的一半是"+s1);
  8. System.out.println("100的十六进制数是"+s);
  9. }
  10. }

正则表达式判断邮箱是否合法

  1. public class Opinion{
  2. public static void main(String args[]) {
  3. //定义正确的qq邮箱格式
  4. String regex="\\w+@\\w+(\\.\\w{2,3})";//"\\w+@\\w+(\\.\\w{2,3})*\\.\\w{2,3}}";
  5. String s1="aaaa@.com";
  6. String s2="aaaa.23a";
  7. String s3="2358903351@qq.com";
  8. if (s1.matches(regex)) {
  9. System.out.println(s1+"是合法的");
  10. }
  11. if(s2.matches(regex)) {
  12. System.out.println(s2+"是合法的");
  13. }
  14. if(s3.matches(regex)) {
  15. System.out.println(s3+"是合法的");
  16. }
  17. }
  18. }

小编第一次写这么多,图片那个胖胖的手指!

不要注意!

不要注意!

不要注意!

如果看到了。。。。。我摊牌了没错就是我!

如果对初学者有帮助,别忘了快夸夸我呀!!!

Java字符串的应用的更多相关文章

  1. Java 字符串格式化详解

    Java 字符串格式化详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 文中如有纰漏,欢迎大家留言指出. 在 Java 的 String 类中,可以使用 format() 方法 ...

  2. (转)Java字符串

    转自:http://blog.sina.com.cn/s/blog_899678b90101brz0.html 创建字符串有两种方式:两种内存区域(字符串池,堆)1," " 引号创 ...

  3. Java字符串split函数的注意事项

    Java字符串的split方法可以分割字符串,但和其他语言不太一样,split方法的参数不是单个字符,而是正则表达式,如果输入了竖线(|)这样的字符作为分割字符串,会出现意想不到的结果, 如, Str ...

  4. 关于java字符串编译优化问题

    情景一:不好的字符串拼接习惯    起因是这样的:一个大牛在写了一篇关于java字符串优化问题的讲解,他提到:不要使用strObj+otherValue的方法将otherValue转换为字符串形式,因 ...

  5. Java字符串排列算法

    Java字符串排列算法 题目:现有ABCDE 5个球 构成的排列组合 可重复抽取 最多取到16个 共有多少种组合方式? 比如:取1个球可以构成的组合有 A B C D E 共5种,取2个球可以构成的组 ...

  6. Java字符串转换

    public class StringConvertToInt{ public static void main(String[] args) { String a ="12a34bW()5 ...

  7. Java字符串null相加

    Java字符串null相加 最近和同事讨论了下面的一段代码: String a = null; a += a; System.out.println(a); 运行结果: nullnull 本着学习的态 ...

  8. JAVA字符串格式化String.format()的使用

    JAVA字符串格式化-String.format()的使用常规类型的格式化 String类的format()方法用于创建格式化的字符串以及连接多个字符串对象.熟悉C语言的同学应该记得C语言的sprin ...

  9. Java字符串的10大热点问题,你都懂吗?

    转自 威哥干JAVA http://www.codingke.com 下面我为大家总结了10条Java开发者经常会提的关于Java字符串的问题,如果你也是Java初学者,仔细看看吧: 1.如何比较字符 ...

  10. java 字符串为空问题

    java 字符串为空问题 String testStr = null; System.out.println(testStr); if (testStr == null) { System.out.p ...

随机推荐

  1. fatal: I don't handle protocol 'git@http' 解决

    新建的git,在git push的时候遇到了报错“fatal: I don't handle protocol 'git@http'” 网上搜这个错误基本都是“fatal: I don't handl ...

  2. 数据库事务练习-Java(新手)

    数据库事务:       一个数据库事务通常包含了一个序列的对数据库的读/写操作.  为数据库操作序列提供了一个从失败中恢复到正常状态的方法,同时提供了数据库即使在异常状态下仍能保持一致性的方法. p ...

  3. Natas0-34 Writeup

    Natas是一个教授服务器端Web安全基础知识的 wargame,通过在每一关寻找Web安全漏洞,来获取通往下一关的秘钥,适合新手入门Web安全. 传送门~ 接下来给大家分享一下,1-34题的Writ ...

  4. Jenkins+Ant+JMeter集成

    Tomcat是jenkins运行的容器,jenkins实际上是依赖于Tomcat才能启动的.Jenkins可以调度ant的脚本. Ant和maven类似,maven是执行pom文件,ant是执行bui ...

  5. abp(net core)+easyui+efcore实现仓储管理系统——入库管理之五(四十一)

    abp(net core)+easyui+efcore实现仓储管理系统目录 abp(net core)+easyui+efcore实现仓储管理系统——ABP总体介绍(一) abp(net core)+ ...

  6. 如何使用pyenv在windows10安装多个python版本环境

    安装pyenv-win pyenv-win的详细内容可以查看源地址 1.这里我用的是第一种安装方法: pip install pyenv-win --target %USERPROFILE%/.pye ...

  7. DRF 序列化组件 模型层中参数补充

    一. DRF序列化 django自带有序列化组件,但是相比rest_framework的序列化较差,所以这就不提django自带的序列化组件了. 首先rest_framework的序列化组件使用同fr ...

  8. settings源码

    自定义settings配置 创建python项目 settings.py NAME = '我是暴露给用户的自定义配置' __init__.py import os import importlib f ...

  9. 破解WIFI教程

    今日主题:如何破解WIFI 准备工具 笔记本一台 usb无线网卡[我用的是小米的] kali系统[可以在虚拟机里装,建议用2019年及以下版本] VMware Workstation15虚拟机安装 可 ...

  10. 常见排序算法总结与分析之交换排序与插入排序-C#实现

    前言 每每遇到关于排序算法的问题总是不能很好的解决,对一些概念,思想以及具体实现的认识也是模棱两可.归根结底,还是掌握不够熟练.以前只是看别人写,看了就忘.现在打算自己写,写些自己的东西,做个总结.本 ...