java關鍵字過濾演算法
❶ 求 JAVA 字元串匹配 完美演算法
只需要實例化 類Matching 設置參數 並調用m.getIndex()方法就OK 請測試... public class Test18{
public static void main(String[] args){
Matching m = new Matching();
m.setOrgStr("ALSKLSHFKDLLS");
m.setSubStr("LS");
System.out.println(m.getIndex());
}
}
class Matching{
String orgStr ="";
String subStr ="";
public void setOrgStr(String orgStr){
this.orgStr = orgStr;
}
public void setSubStr(String subStr){
this.subStr = subStr;
}
public String getIndex(){
StringBuffer sb = new StringBuffer("{");
//根據關鍵字subStr來拆分字元串orgStr所得的字元串數組
String[] sub = orgStr.split(subStr);
int keyLength = subStr.length(); //關鍵字長度
int keySize = 0; //關鍵字個數
int subSize = sub.length; //子字元串個數
int subLength = 0; //子字元串長度
if(!orgStr.endsWith(subStr)){
keySize = subSize-1;
}else
keySize = subSize; int[] index = new int[keySize];//關鍵字下標數組
for(int i=0;i<keySize;i++){
subLength = sub[i].length();
if(i==0){
index[i]=subLength;
}else
index[i]=index[i-1]+subLength+keyLength;
}
if(keySize>0){
int l = keySize-1;
for(int i=0;i<l;i++){
sb.append(index[i]+",");
}
sb.append(index[l]);//最後一個關鍵字下標
}else{
sb.append("NULL");
}
sb.append("}");
return sb.toString();
}
}
❷ 求高效率的 java+mysql 關鍵字過濾代碼
你金幣都不獎,叫別人出力,可能嗎?我也想要啊。。。
❸ 求過濾關鍵字的正則表達式JAVA
String str="fa證迷襲";
String regEx="[^辦證迷葯]*";
boolean flag=str.matches(regEx);
System.out.println(flag);
❹ java關鍵字查詢演算法
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.File;
public class search
{
//查找方法,參數,文件絕對路徑,查找關鍵字
public static boolean search(String filepath,String key)
{
try
{
File f = new File(filepath);
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String s = "";
//int i = 1;
while((s = br.readLine()) != null)
{
if(s.indexOf(key) != -1)
{
return true;
}
}
return false;
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
}
public static void main(String args[])
{
System.out.println(search.search("d://t.txt","l2"));
}
}
修改了下,加兩個變數,可以指出查找的位置。
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.File;
public class search
{
//查找方法,參數,文件絕對路徑,查找關鍵字
public static String search(String filepath,String key)
{
try
{
File f = new File(filepath);
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String s = "";
int i = 1;
int m = 0;
while((s = br.readLine()) != null)
{
if((m = s.indexOf(key)) != -1)
{
return "第"+i+"段,第"+m+"處";
}
i++;
}
return null;
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
}
public static void main(String args[])
{
System.out.println(search.search("d://t.txt","asd"));
}
}
這個,查漢字是沒有問題的。
另外,你要全文檢索的話,indexOf()還有個方法,indexOf(int start,String key),指定開始查找的位置跟關鍵字,你查到一處後,將這個數值加1,做為繼續查找的開始位置就可以了。
❺ java編程 如何讓實現過濾 「臟字」
只能枚舉你要過濾的所有「臟字」了。最好寫在一個文件裡面,不要直接用數組。
寫個類完成將「臟字」替換為「*」號的邏輯。
加個過濾器,攔截提交留言的請求,並調用邏輯。
❻ 高分求一個使用Java Hashtable的關鍵字過濾小程序
這個應該就是你想要達到的效果吧 ??
添加了個程序運行時間,不過像這樣的一點文本,一般幾乎不會用時間的
import java.util.Hashtable;
public class TestStr {
public static boolean getStr(String text,String str[]){
Hashtable map=new Hashtable();
char c[]=text.toCharArray();
for (int i = 0; i < str.length; i++) {
map.put(str[i].trim(), str[i].trim());
}
int count=0;
for (int i = 0; i < c.length; i++) {
if(map.get(c[i]+"")!=null){
count++;
}
}
System.out.println("關鍵字出現次數"+count);
return count > 0;
}
public static void main(String[] args) {
//預設的文本內容
String text=aa@bb%cc*dd$;
//預設的關鍵字
String str[]={"@","%","*","$"};
double start = System.currentTimeMillis() ;
boolean bl=TestStr.getStr(text, str);
double end = System.currentTimeMillis();
System.out.println("查詢執行時間: " + (end - start));
if(bl){
System.out.println("文本中出現關鍵字");
}else{
System.out.println("文本中未出現關鍵字");
}
}
}
❼ 在JAVA中怎麼實現關鍵字過濾
自己判斷一下, 字元串中是否包含某個關鍵字即可, String.contains(CharSequence s) 如果存在返回true, 否則返回false
❽ 簡訊關鍵字過濾演算法有哪些
bool CKeyWordManager::find(const std::string &key){ for (int n = 0; n < key.length (); ++n) { for (int i = 1; i < m_keyWordMaxLength && i + n < key.length (); ++i) { set <string>::iterator it = m_keWordList.find (key.substr (n, i)); if (it != m_keyWordList.end ()) return true; } } return false;}