java的字符的匹配和替换
2014-06-07 15:14
197 查看
/** *测试类,测试两个功能类,MatchBrote_force类和subString_act类的功能实现 *创建的类为TestMatchBrote_force *@author LZ *@vesion 6.2 */ //import com.edu.software.LZ.*; public class TestMatchBrote_force { public static void main(String[] args){ /** *新建对象调用MatchBrote_force类 *得到字串在主串中的位置 */ int pos = 0; //声明一个类变量M MatchBrote_force M; //创建对象,创建空间 M= new MatchBrote_force(); //初始化主串 String str = "hello world!i love you.Data Structure!" ; //初始化字串 String str1 = "Structure"; pos=M.MatchBrote(str,str1); /** *此处是为了调用类MatchBrote_foce *是否能够找到字串 */ System.out.println("第一题:能否找到该字符串!\n"); if(pos>0){ System.out.println("找到了字串在主串中的位置!\n"); }else{ System.out.println("没有找到这样的字串,请重新核查!\n"); } /** *找出字串在主串中的位置并返回字串在主串中 *第一次出现的下表 */ System.out.println("第二题:返回字串在主串中第一次出现的第一个字符的下标!\n"); /** *判断主串是否存在 * */ if(pos>0){ System.out.println("找到了字串在主串中的位置,其出现的下标是:\n"+pos); }else{ System.out.println("没有找到这样的字串,请重新核查!\n"+pos); } System.out.println("第三题:替换前和替换后的字符串!\n"); /** *新建对象调用SubString类,获取替换后的主串 */ //声明SubString类的变量S SubString S; //申请空间,创建对象 S = new SubString(); //定义要替换的目标字符串 String swap = "prepare well"; S.subString_act(str,str1 ,swap); } }
//package com.edu.software.LZ; /** * 模式匹配 * @author LZ * @version 6.2 * 创建MatchBrote_force类用来做匹配操作 *如果成功匹配返回Index_first,表示第一次出现的下标,反之返回-1 */ class MatchBrote_force{ /** *创建无参的构造方法 *@param 无 *@return 无 */ public MatchBrote_force(){} /** *创建串的匹配算法的方法 *功能:找出字串在主串中的位值并返回字串在主串中的位置 *@param mainString 为主串,matchString 为字串 *@return 返回字串在主串中的位置 */ public int MatchBrote(String mainString,String matchString ){ System.out.println("count"); //判断传入的参数是否为空或不合法 if(mainString == null || matchString == null || matchString.length() > mainString.length() ){ System.out.println("参数异常,请重新确定参数!"); return -1; } /** *匹配算法的核心算法 *若是找到了字串在主串中返回字串出现在主串中的第一个字符的下表Index_first *反之则返回0 表示没有找到这样的字串 */ int ma_pos ;//表示比较时主串中比较到的位置 int mt_pos;//表示比较时字串所比较到的位置 int Index_first =0;//表示存储返回字串在主串中出现的第一个字符的下标 //算法实现 //外循环用来外串中的比较 outer: for(ma_pos = 0;ma_pos <mainString.length();ma_pos++){ //内循环用来比较字串比较的操作 inner: for(mt_pos = 0;mt_pos <matchString.length();mt_pos++){ //如果不匹配跳出内层循环 if(mainString.charAt(ma_pos+mt_pos) != matchString.charAt(mt_pos) ){ break inner; } //如果字串长度与匹配的次数相等说明存在此字符串,返回其所在主串中的出现位置Index_first if(mt_pos == matchString.length()-1){ Index_first = ma_pos; //return ma_pos;//返回成功的第一个位置 }else{ //如果不存在此字符串,返回-1 Index_first = -1; } } } return Index_first; } }
//package com.edu.software.LZ; /** * 模式匹配 * @author LZ * @version 6.2 * 创建MatchBrote_force类用来做匹配操作 *如果成功匹配返回Index_first,表示第一次出现的下标,反之返回-1 */ class MatchBrote_force{ /** *创建无参的构造方法 *@param 无 *@return 无 */ public MatchBrote_force(){} /** *创建串的匹配算法的方法 *功能:找出字串在主串中的位值并返回字串在主串中的位置 *@param mainString 为主串,matchString 为字串 *@return 返回字串在主串中的位置 */ public int MatchBrote(String mainString,String matchString ){ System.out.println("count"); //判断传入的参数是否为空或不合法 if(mainString == null || matchString == null || matchString.length() > mainString.length() ){ System.out.println("参数异常,请重新确定参数!"); return -1; } /** *匹配算法的核心算法 *若是找到了字串在主串中返回字串出现在主串中的第一个字符的下表Index_first *反之则返回0 表示没有找到这样的字串 */ int ma_pos ;//表示比较时主串中比较到的位置 int mt_pos;//表示比较时字串所比较到的位置 int Index_first =0;//表示存储返回字串在主串中出现的第一个字符的下标 //算法实现 //外循环用来外串中的比较 outer: for(ma_pos = 0;ma_pos <mainString.length();ma_pos++){ //内循环用来比较字串比较的操作 inner: for(mt_pos = 0;mt_pos <matchString.length();mt_pos++){ //如果不匹配跳出内层循环 if(mainString.charAt(ma_pos+mt_pos) != matchString.charAt(mt_pos) ){ break inner; } //如果字串长度与匹配的次数相等说明存在此字符串,返回其所在主串中的出现位置Index_first if(mt_pos == matchString.length()-1){ Index_first = ma_pos; //return ma_pos;//返回成功的第一个位置 }else{ //如果不存在此字符串,返回-1 Index_first = -1; } } } return Index_first; } }
相关文章推荐
- java中替换一些特殊字符的办法
- JAVA中替换一些特殊字符的方法
- java 替换特殊字符
- java中替换字符以及回车换行
- Notepad++中的正则_替换匹配串中的字符
- Javascript 替换全部匹配字符
- 【Java语言】Java 字符替换效率比较
- Java 正则表达式--字符串匹配、切割、与替换
- replace和replaceAll是JAVA中常用的替换字符的方法
- JAVA正则表达式匹配,替换,查找,切割
- Java正则表达式匹配、替换HTML内容[使用Jakarta ORO]
- java字符窜替换
- 字符串匹配替换(指定匹配位置)
- Java 正则表达式--匹配字符获取、IP地址排序
- java特殊字符替换
- 对每一个匹配的部分替换不同的字符
- JAVA正则表达式匹配,替换,查找,切割
- Java正则表达式匹配、替换HTML内容
- Java字符串工具类(字符串 替换 分隔符 特殊字符处理)
- java正则表达式来替换文本中匹配正则的内容