java实现串的匹配和替换
?/**
*測試類,測試兩個功能類,MatchBrote_force類和subString_act類的功能實現(xiàn)
*創(chuàng)建的類為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;
//創(chuàng)建對象,創(chuàng)建空間
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");
}
/**
*找出字串在主串中的位置并返回字串在主串中
*第一次出現(xiàn)的下表
*/
System.out.println("第二題:返回字串在主串中第一次出現(xiàn)的第一個字符的下標!\n");
/**
*判斷主串是否存在
*
*/
if(pos>0){
System.out.println("找到了字串在主串中的位置,其出現(xiàn)的下標是:\n"+pos);
}else{
System.out.println("沒有找到這樣的字串,請重新核查!\n"+pos);
}
System.out.println("第三題:替換前和替換后的字符串!\n");
/**
*新建對象調用SubString類,獲取替換后的主串
*/
//聲明SubString類的變量S
SubString S;
//申請空間,創(chuàng)建對象
S = new SubString();
//定義要替換的目標字符串
String swap = "prepare well";
S.subString_act(str,str1 ,swap);
}
}
?
?
?
?
?
//package com.edu.software.LZ;
/**
* 模式匹配
* @author LZ
* @version 6.2
* 創(chuàng)建MatchBrote_force類用來做匹配操作
*如果成功匹配返回Index_first,表示第一次出現(xiàn)的下標,反之返回-1
*/
class MatchBrote_force{
/**
*創(chuàng)建無參的構造方法
*@param 無
*@return 無
*/
public MatchBrote_force(){}
/**
*創(chuàng)建串的匹配算法的方法
*功能:找出字串在主串中的位值并返回字串在主串中的位置
*@param mainString 為主串,matchString 為字串
*@return 返回字串在主串中的位置
*/
public int MatchBrote(String mainString,String matchString ){
System.out.println("count");
//判斷傳入的參數(shù)是否為空或不合法
if(mainString == null || matchString == null || matchString.length() > mainString.length() ){
System.out.println("參數(shù)異常,請重新確定參數(shù)!");
return -1;
}
/**
*匹配算法的核心算法
*若是找到了字串在主串中返回字串出現(xiàn)在主串中的第一個字符的下表Index_first
*反之則返回0 表示沒有找到這樣的字串
*/
int ma_pos ;//表示比較時主串中比較到的位置
int mt_pos;//表示比較時字串所比較到的位置
int Index_first =0;//表示存儲返回字串在主串中出現(xiàn)的第一個字符的下標
//算法實現(xiàn)
//外循環(huán)用來外串中的比較
outer:
for(ma_pos = 0;ma_pos <mainString.length();ma_pos++){
//內循環(huán)用來比較字串比較的操作
inner:
for(mt_pos = 0;mt_pos <matchString.length();mt_pos++){
//如果不匹配跳出內層循環(huán)
if(mainString.charAt(ma_pos+mt_pos) != matchString.charAt(mt_pos) ){
break inner;
}
//如果字串長度與匹配的次數(shù)相等說明存在此字符串,返回其所在主串中的出現(xiàn)位置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
* 創(chuàng)建MatchBrote_force類用來做匹配操作
*如果成功匹配返回Index_first,表示第一次出現(xiàn)的下標,反之返回-1
*/
class MatchBrote_force{
/**
*創(chuàng)建無參的構造方法
*@param 無
*@return 無
*/
public MatchBrote_force(){}
/**
*創(chuàng)建串的匹配算法的方法
*功能:找出字串在主串中的位值并返回字串在主串中的位置
*@param mainString 為主串,matchString 為字串
*@return 返回字串在主串中的位置
*/
public int MatchBrote(String mainString,String matchString ){
System.out.println("count");
//判斷傳入的參數(shù)是否為空或不合法
if(mainString == null || matchString == null || matchString.length() > mainString.length() ){
System.out.println("參數(shù)異常,請重新確定參數(shù)!");
return -1;
}
/**
*匹配算法的核心算法
*若是找到了字串在主串中返回字串出現(xiàn)在主串中的第一個字符的下表Index_first
*反之則返回0 表示沒有找到這樣的字串
*/
int ma_pos ;//表示比較時主串中比較到的位置
int mt_pos;//表示比較時字串所比較到的位置
int Index_first =0;//表示存儲返回字串在主串中出現(xiàn)的第一個字符的下標
//算法實現(xiàn)
//外循環(huán)用來外串中的比較
outer:
for(ma_pos = 0;ma_pos <mainString.length();ma_pos++){
//內循環(huán)用來比較字串比較的操作
inner:
for(mt_pos = 0;mt_pos <matchString.length();mt_pos++){
//如果不匹配跳出內層循環(huán)
if(mainString.charAt(ma_pos+mt_pos) != matchString.charAt(mt_pos) ){
break inner;
}
//如果字串長度與匹配的次數(shù)相等說明存在此字符串,返回其所在主串中的出現(xiàn)位置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
總結
以上是生活随笔為你收集整理的java实现串的匹配和替换的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 『物理社工』五一回家的“小插曲”
- 下一篇: django - 替换admin的tex