字符串提取和查询方法

public int indexOf(String value) //搜索第一个出现的字符串 value,如果没找到返回-1

public int indexOf(int ch) //搜索第一个出现的字符串ch,如果没找到返回-1
public int lastIndexOf(int ch)//搜索第一个出现的字符ch,如果没找到返回-1
public int lastIndexOf(String value) //搜索最后一个出现的字符ch(或字符串 value)如果没有找到返回-1
public String substring(int index) //提取从索引位置开始的字符串部分(直到末尾)
public String substring(int beginindex,int endindex) //提取beginindex和endindex之间的字符串部分 包含beginindex,但不包含endindex
public String trim() //返回一个前后不含空格的调用字符串的副本

实列

学生使用作业提交系统提交Java作业时,输入Java原文件名,并输入自己的电子邮箱,提交前系统检查:是否符合Java源文件名;电子邮箱是否为合法地电子邮箱。此代码实现提交前验证功能。

/**
 * 
 */
package zut.edu.cs.network.practice;

import java.util.Scanner;

/**
 * @author Administrator
 *
 */
public class Verify {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		boolean fileCorrect=false;
		boolean emailCorrect=false;
		System.out.println("welcome to the system of handing in homework");
		@SuppressWarnings("resource")
		Scanner input=new Scanner(System.in);
		System.out.println("please input \".java's \"file name");
		String  fileName=input.next();
		System.out.println("please inpute the email:");
		String email=input.next();
		//verify java file name
		int index=fileName.lastIndexOf(".");
		if(index!=-1&&index!=0&&fileName.substring(index+1,fileName.length() ).equals("java")) {
			fileCorrect=true;  //marked file name correct
		}
		else {
			System.out.println("file name invalid!");
		}
		//verify email name
		if(email.indexOf('@')!=-1&&email.indexOf('.')>email.indexOf('@')) {
			emailCorrect=true;   //marked email correct
		}
		else {
			System.out.println("E-mail invalid!");
		}
		//output the result 
		if(fileCorrect&&emailCorrect) {
			System.out.println("hand in homework successfully!");
		}
		else {
			System.out.println("failed to hand in honework");
		}
	}

}

 

运行截图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值