日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java实现串的匹配和替换

發(fā)布時間:2024/10/12 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java实现串的匹配和替换 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?/**

*測試類,測試兩個功能類,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实现串的匹配和替换的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。