java实现串的匹配和替换

 /**

*测试类,测试两个功能类,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("第一题:能否找到该字符串! ");


if(pos>0){

System.out.println("找到了字串在主串中的位置! ");

}else{

System.out.println("没有找到这样的字串,请重新核查! ");

}

/**
*找出字串在主串中的位置并返回字串在主串中
*第一次出现的下表
*/

System.out.println("第二题:返回字串在主串中第一次出现的第一个字符的下标! ");

/**
*判断主串是否存在
*
*/

if(pos>0){

System.out.println("找到了字串在主串中的位置,其出现的下标是: "+pos);

}else{

System.out.println("没有找到这样的字串,请重新核查! "+pos);

}

System.out.println("第三题:替换前和替换后的字符串! ");

/**
*新建对象调用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;

}



}



原文地址:https://www.cnblogs.com/qpxv/p/3774819.html