Java字符串的应用
字符串的简单应用
public class join {
public static void main (String args[]){
String s1 = new String ("hello");
String s2 = new String ("word");
String s = s1 + s2;
int size= s.length();
System.out.println(size);
}
}//输出字符串长度
/*public class join {
public static void main (String args[]){
String s1 = new String ("hello");
String s2 = new String ("word");
String s = s1 + " " + s2;
System.out.println(s);
}
}//连接多个字符串*/
/*public class join {
public static void main (String args[]){
String s1 = new String ("hello");
String s2 = new String ("word");
String s = s1 + s2;
int size= s.indexOf("e"); //在这里可以修改局部变量 "e" 可以改变成其他的浮点
System.out.println(size);
}
}//字符串的查找
*/
/*public class join {
public static void main (String args[]){
String s1 = new String ("hello");
String s2 = new String ("word");
String s = s1 + s2;
int size= s.lastIndexOf(""); //""中间没有空格表示空字符串
System.out.println("空字符在字符串中的索引位置"+size);
System.out.println("字符串的长度是" + s.length());
}
}//查找空字符串的位置并输出长度*/
连接其他数据类型(Link型)
public class Link {
public static void main(String args[]) {
int booktime = 4;
float practicetime = 2.5f;
System.out.println( "我每天花费 " + booktime + "小时看书;" + practicetime + "小时练习");
}
}
索引位置x的对应位置的字母是什么
public class join {
public static void main (String args[]){
String s1 = new String ("hello");
String s2 = new String ("word");
String s = s1 + " " + s2;
char j =s.charAt(6);
System.out.println("索引位置6所对的字母是"+j);
}
}
获取字符串
public class join {
public static void main (String args[]){
String s1 = new String ("hello");
String s2 = new String ("word");
String s = s1+s2;
System.out.println(s);
String y=s.substring(0, 3);
System.out.println(y);
}
}
去除空格
public class join {
public static void main (String args[]){
String s1 = new String ("hello");
String s2 = new String ("word");
String s = " "+s1+" "+s2+" ";
System.out.println("原来的字符串长度为"+s.length());
System.out.println("去掉空格后的字符串长度为"+s.trim().length());
}
}
字符串的替换
public class join {
public static void main (String args[]){
String s1 = new String ("hello");
String s2 = new String ("word");
String s = " "+s1+" "+s2+" ";
System.out.println(s);
System.out.println("替换之后的"+s.replace("word", "java"));
}
}
替换之后是hello java
判断字符串的开始和结尾
public class startorend {
public static void main(String args[]) {
String num1="12345678";
String num2="23456789";
boolean b=num1.startsWith("12");
boolean c=num1.endsWith("89");
boolean d=num2.startsWith("12");
boolean e=num2.endsWith("89");
System.out.println("num1输出的是否以12开始"+b);
System.out.println("num1输出的是否以89结束"+c);
System.out.println("num2输出的是否以12开始"+d);
System.out.println("num1输出的是否以89结束"+e);
}
public startorend() {
// TODO 自动生成的构造函数存根
}
}
用equal 和equalignore判断字符串是否相等
public class Opinion {
public static void main(String args[]) {
String a=new String("abc");
String b=new String ("ABC");
String c=new String ("abc");
boolean x=a.equals(b);
boolean y=a.equalsIgnoreCase(b);
System.out.println(x);
System.out.println(y);
}
public Opinion() {
// TODO 自动生成的构造函数存根
}
}
按照字典顺序比较两个字符串
public class Opinion{
public static void main(String args[]) {
String s1="a";
String s2="b";
String s3="c";
System.out.println(s1 +" "+"comepareTo"+" "+s2+s1.compareTo(s2));
System.out.println(s1+" "+"comepareTo"+" "+s3+s1.compareTo(s3));
}
}
字符串字母大小写转换
public class Opinion{
public static void main(String args[]) {
String s1="abc";
String s2="LZB";
String s=s1+s2;
String news1=s.toLowerCase();
String news2=s.toUpperCase();
System.out.println(s);
System.out.println(news1+news2);
}
}
日期和时间字符串格式化
import java.util.Date;
@SuppressWarnings("unused")
public class date {
public static void main(String[] args) {
Date days=new Date();//创建DATE的类day
String year =String.format("%tY", days);
String month=String.format("%tB", days);
String day=String.format("%td", days);
System.out.println("今年是"+year+"年");
System.out.println("现在是"+month);
System.out.println("今天是"+day+"号");
}
public date() {
// TODO 自动生成的构造函数存根
} }



时间格式化
import java.util.Date;
public class date {
public static void main(String[] args) {
Date time=new Date();//创建DATE的类time
String hour =String.format("%tH", time);
String minute=String.format("%tM", time);
String second=String.format("%tS", time);
System.out.println("现在的时间是"+hour+"时"+minute+"分"+second+"秒");
}
public date() {
// TODO 自动生成的构造函数存根
} }
import java.util.Date;
public class date {
public static void main(String[] args) {
Date days=new Date();//创建DATE的类time
String day =String.format("%tc", days);
String form=String.format("%tF", days);
System.out.println("全部的时间信息是"+day);
System.out.println("年-月-日格式"+form);
}
public date() {
// TODO 自动生成的构造函数存根
} }
实验结果:
全部的时间信息是星期二 二月 04 09:54:45 CST 2020
年-月-日格式2020-02-04 常规类型格式化
public class Opinion{
public static void main(String args[]) {
String s1=String.format("%d", 400/2);
String s2=String.format("%b", 2>7);
String s=String.format("%x", 100);
System.out.println("2>7"+s2);
System.out.println("400的一半是"+s1);
System.out.println("100的十六进制数是"+s);
}
}

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

public class Opinion{
public static void main(String args[]) {
//定义正确的qq邮箱格式
String regex="\\w+@\\w+(\\.\\w{2,3})";//"\\w+@\\w+(\\.\\w{2,3})*\\.\\w{2,3}}";
String s1="aaaa@.com";
String s2="aaaa.23a";
String s3="2358903351@qq.com";
if (s1.matches(regex)) {
System.out.println(s1+"是合法的");
}
if(s2.matches(regex)) {
System.out.println(s2+"是合法的");
}
if(s3.matches(regex)) {
System.out.println(s3+"是合法的");
}
}
}

小编第一次写这么多,图片那个胖胖的手指!
不要注意!
不要注意!
不要注意!
如果看到了。。。。。我摊牌了没错就是我!
如果对初学者有帮助,别忘了快夸夸我呀!!!
Java字符串的应用的更多相关文章
- Java 字符串格式化详解
Java 字符串格式化详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 文中如有纰漏,欢迎大家留言指出. 在 Java 的 String 类中,可以使用 format() 方法 ...
- (转)Java字符串
转自:http://blog.sina.com.cn/s/blog_899678b90101brz0.html 创建字符串有两种方式:两种内存区域(字符串池,堆)1," " 引号创 ...
- Java字符串split函数的注意事项
Java字符串的split方法可以分割字符串,但和其他语言不太一样,split方法的参数不是单个字符,而是正则表达式,如果输入了竖线(|)这样的字符作为分割字符串,会出现意想不到的结果, 如, Str ...
- 关于java字符串编译优化问题
情景一:不好的字符串拼接习惯 起因是这样的:一个大牛在写了一篇关于java字符串优化问题的讲解,他提到:不要使用strObj+otherValue的方法将otherValue转换为字符串形式,因 ...
- Java字符串排列算法
Java字符串排列算法 题目:现有ABCDE 5个球 构成的排列组合 可重复抽取 最多取到16个 共有多少种组合方式? 比如:取1个球可以构成的组合有 A B C D E 共5种,取2个球可以构成的组 ...
- Java字符串转换
public class StringConvertToInt{ public static void main(String[] args) { String a ="12a34bW()5 ...
- Java字符串null相加
Java字符串null相加 最近和同事讨论了下面的一段代码: String a = null; a += a; System.out.println(a); 运行结果: nullnull 本着学习的态 ...
- JAVA字符串格式化String.format()的使用
JAVA字符串格式化-String.format()的使用常规类型的格式化 String类的format()方法用于创建格式化的字符串以及连接多个字符串对象.熟悉C语言的同学应该记得C语言的sprin ...
- Java字符串的10大热点问题,你都懂吗?
转自 威哥干JAVA http://www.codingke.com 下面我为大家总结了10条Java开发者经常会提的关于Java字符串的问题,如果你也是Java初学者,仔细看看吧: 1.如何比较字符 ...
- java 字符串为空问题
java 字符串为空问题 String testStr = null; System.out.println(testStr); if (testStr == null) { System.out.p ...
随机推荐
- 机器学习 - LSTM应用之情感分析
1. 概述 在情感分析的应用领域,例如判断某一句话是positive或者是negative的案例中,咱们可以通过传统的standard neuro network来作为解决方案,但是传统的神经网络在应 ...
- Redis 服务端程序实现原理
上篇我们简单介绍了 redis 客户端的一些基本概念,包括其 client 数据结构中对应的相关字段的含义,本篇我们结合这些,来分析分析 redis 服务端程序是如何运行的.一条命令请求的完成,客户端 ...
- Java-用集合存储对象(新手)
//导入的包.import java.util.ArrayList;//用集合存储对象,遍历集合,取所有元素. 用get方法.//创建的一个类.public class zylx4 { //公共静态的 ...
- Flutter配置环境报错“PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target”
背景:最近看了很多Flutter漂亮的项目,想要尝试一下.所有环境都搭建好之后,按照文档一步一步配置(抄袭),但始终报如下图错误. PKIX path building failed: sun.sec ...
- centOS6.5桌面版用不了中文输入法解决方案
1:centos6.5中 系统->首选项->输入法中选择“使用iBus(推荐)”,点击首选输入法n遍,没有任何效果. 2.我也弄了很多种方式包括用 yum install " ...
- 如何使用pyenv在windows10安装多个python版本环境
安装pyenv-win pyenv-win的详细内容可以查看源地址 1.这里我用的是第一种安装方法: pip install pyenv-win --target %USERPROFILE%/.pye ...
- 简单BBS项目开始(二)
登陆和生成图片验证码 1.生成图片 pillow 1.生成图片的模块pillow,在python中安装pillow,在Django中使用时用PIL2. 在页面上<img id="val ...
- CF1324A Yet Another Tetris Problem 题解
原题链接 简要题意: 再简要一波: 每次可以把一个数增加 \(2\),问最后能不能让所有数相等.(也就是抵消掉) 什么?题意变成这样子还做个啥? 你会发现,必须所有数的奇偶性都相同,才可以:反之就不可 ...
- Inception V1-V4
2019-05-29 20:56:02 一.Inception V1 当不知道在卷积神经网络中该使用1 * 1卷积还是3 * 3的卷积还是5 * 5的卷积或者是否需要进行pooling操作的时候,我们 ...
- 爬虫&Selenium&ChromeDriver
一.Selenium selenium是什么 Selenium [1] 是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE(7, ...
