当前位置:网站首页>【字符串中处理类String的使用】
【字符串中处理类String的使用】
2022-08-06 23:49:00 【*年夕】
解析一个邮箱地址是否合法,如果合法则打印出用户名部分和该邮箱所属的网站域名,如果邮箱地址不合法则显示不合法的原因
1 提示:邮箱地址不合法的因素:
1.1 邮箱地址中不包含@或.
1.2 邮箱地址中含有多了@或.
1.3 邮箱地址中.出现在@的前面
1.4 用户名里有其他字符
2 实现步骤:
2.1 创建一个类
package aaaa;
import java.util.Scanner;
public class J邮箱地址 {
public static void main(String[] args) {
System.out.println("请输入一个邮箱地址: ");
Scanner sc=new Scanner(System.in);
String x=sc.next();
if(Mail(x)==true) {
}else {
System.out.println("不合法");
}
}
public static boolean Mail(String x) {
boolean b=true;
if(x.indexOf('@')==-1||x.indexOf('.')==-1) {
System.out.println("邮箱地址中不包含@或.");
return false;
}
if(x.indexOf('@')!=x.lastIndexOf('@')||x.indexOf('.')!=x.lastIndexOf('.')) {
System.out.println("邮箱地址中含有多了@或.");
return false;
}
if(x.lastIndexOf('@')>x.lastIndexOf('.')) {
System.out.println("邮箱地址中.出现在@的前面");
return false;
}
for(int i=0;i<x.indexOf('@');i++) {
char c=x.charAt(i);
if((c>=48&&c<=57)||(c>=65)&&(c<=90)||(c>=97)&&(c<=122)) {
}
else {
System.out.println("用户名里有其他字符");
return false;
}
}
String[] arr=x.split("@");
System.out.println("用户名为:"+arr[0]);
System.out.println("该邮箱所属的网站域名为:"+arr[1]);
return true;
}
}
结果截图:
int indexOf/lastIndexOf(char)
返回指定字符在此字符串中第一次/最后一次出现处的索引。
char charAt(int)
返回指定索引处的 char 值
String[] split(String regex)
根据给定正则表达式的匹配拆分此字符串。
边栏推荐
- SmartIDE v1.0.23 一个非常不敏捷的发布
- Rebuild binary tree
- GoLang系统设计
- leetcode 24. 两两交换链表中的节点
- 基于FTP协议文件的上传与下载
- PAT Grade B-B1026 Program Run Time (15)
- [Combat of High Concurrency Projects] Principle Analysis of Engineering Modularity and Static Architecture of Event Venues
- js中的原型与原型链
- 【超好懂的比赛题解】HNCPC Multi-university Training Round3 比赛题解
- Uuid 32-bit data processing, 16
猜你喜欢
随机推荐
What is the matter with several IP addresses of this machine?to analyze
bugku easy_nbt
网络通信之NIO编程
深入了解集合的使用方法
推荐一款管理系统专用低代码工具,一天开发一个系统不是梦!
PAT serie b - B1024 scientific notation (20)
Day120.尚医通:项目总结
面试遇见简单算法总结
MySql操作之DML
Easily complete interface testing and interface documentation
JCF之List集合实现——Vector
ansible 一题多解
Empty suite
CSV文件如何使用EXCEL打开
抓取商品上传提示“图片宽度不能大于5000,长度不能大于5000,请修改!”,怎么解决?
Promise的点点滴滴
机器学习 | MATLAB实现支持向量机分类ClassificationSVM参数设定
设计电商秒杀系统
居延安《公共关系学》重点整理
openharmony萌新贡献指南








