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");
}
}
}
运行截图: