写一个去除文章里某些固定的字符串的java程序
\uff08Java\uff09\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f,\u4ece\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\u4e2d\u5220\u9664\u6240\u6709\u51fa\u73b0\u67d0\u4e2a\u6307\u5b9a\u5b57\u7b26\u4e32\u7684\u5730\u65b9\u989d\uff0c\uff0c\u56e0\u4e3a\u4f60\u64cd\u4f5c\u7684\u662f\u540c\u4e00\u4e2a\u6587\u4ef6\uff0c\uff0c\uff0c
\u5f53PrintWriter output=new PrintWriter(file);
\u7684\u65f6\u5019\uff0c\u5047\u5982\u6587\u4ef6\u5b58\u5728\u4f1a\u6e05\u7a7a\u3002\u3002\u3002\u3002\u6240\u4ee5\u4f60\u4ec0\u4e48\u90fd\u8bfb\u4e0d\u5230\u3002\u3002
\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u5c31\u662f\u8bfb\u53d6\u8f93\u5165\u6d41\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7136\u540e
public void remove() {String s = "This is a text file, some text would be removed, text left would be kept";String result = s.replaceAll("text", "");System.out.println(result);}
public static void main(String[] args) {
String s1="abcdefg abcdewq fdgkj,abcdefg,befuasx";//你的文章
String s2="abcdefg";//要被替换的关键字
String s3="";//要替换成的关键字 去除就意味着替换成空字符串
System.out.println(s1);//输出替换前的原文章
s1=s1.replace(s2, s3);//替换操作
System.out.println(s1);//输出替换后的结果
}
}
结果:
abcdefg abcdewq fdgkj,abcdefg,befuasx
abcdewq fdgkj,,befuasx
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class ReplaceText {
public static void main(String[] args ) throws Exception {
File source = new File("d:/abc.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(source)));
File desc = new File("d:/a.txt");
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(desc)));
String str = null;
while ((str = in.readLine()) != null) {
str = str.replace("abcdefg", "");
out.write(str + "\r\n");
}
out.close();
in.close();
source.delete();
desc.renameTo(source);
}
}
感觉用 substring 与indexof 两个方法应该可以
绛旓細include<stdio.h>#define MAX 1000int getline(char [],int );int strindex(char [],char []);int main(void){char line[MAX];char pattern[MAX];printf("渚濇杈撳叆鏂囨湰鍜屽尮閰嶇殑鏂囨湰锛屾寜鍥炶溅缁撴潫杈撳叆锛岄鍑烘寜涓や笅鍥炶溅\n");while(getline(line,MAX)&&getline(pattern,MAX)){if(strindex(line,...
绛旓細鐢ㄢ滄煡鎵炬浛鎹⑩濆姛鑳斤紒 鍦╓ORD绐楀彛涓婇潰鐐光滅紪杈戔濄佲滄浛鎹⑩濄 寮瑰嚭鐨勭獥鍙d腑锛岀偣鈥滈珮绾р濆睍寮鏁翠釜绐楀彛銆 鈥滄悳绱㈣寖鍥粹濅负鍏ㄩ儴锛屽皢鈥滃尯鍒嗗叏/鍗婅鈥濆墠鐨勫嬀鍘绘帀銆傚叾瀹冮夐」涓嶈鎵撳嬀銆 灏嗗厜鏍囧畾浣嶅湪鈥滄煡鎵惧唴瀹光濇梺鐨勮緭鍏ユ鍐咃紝鍐嶇偣鈥滅壒娈瀛楃鈥濓紝閫夋嫨鈥滅櫧鑹茬┖鏍尖濄 浣库滄浛鎹负鈥濇梺鐨勮緭鍏ユ淇濇寔涓虹┖銆 鎸...
绛旓細涓嬫媺浠ュ悗鐪嬪埌杩欎竴缁勬暟鎹兘鍙繚鐣欎簡鏁板瓧閮ㄥ垎锛屽凡缁鍘绘帀鏈鍚涓涓瀛楃銆傚嚱鏁拌В閲婏細left鍑芥暟琛ㄧず鎴彇瀛楃涓插乏渚х殑閮ㄥ垎瀛楃銆俵en鍑芥暟琛ㄧず璁$畻瀛楃涓鐨勫瓧绗鏁般傚叾涓璴en(A1)-1琛ㄧず姣攍en璁$畻鐨勫瓧绗︽暟灏1銆傝宭eft鍒欐埅鍙栫殑瀛楃灏辨槸浠庡乏渚х畻璧峰埌鏈鍚庝竴浣嶅瓧绗︾殑鍓嶄竴涓瓧绗︿负姝傜浉鍏鏂囩珷闃呰锛
绛旓細1銆佸熀鏈浛鎹㈢敤word鏇挎崲鏂囨湰鏄緢绠鍗曠殑锛氶鍏堟墦寮涓涓鏂囨。锛屾墽琛屸滅紪杈戜竴銆夋浛鎹⑩(鎴栨寜鈥淐trl+H鈥濈粍鍚堥敭)锛屾墦寮鈥滄煡鎵惧拰鏇挎崲鈥濆璇濇锛屽湪鈥滄煡鎵惧唴瀹光濆彸渚х殑鏂规涓緭鍏ラ渶瑕佹浛鎹鐨勫瓧绗锛屽湪鈥滄浛鎹负鈥濆彸渚х殑鏂规涓緭鍏ユ浛鎹㈠悗鐨勫瓧绗︼紝鐒跺悗鍗曞嚮鈥滃叏閮ㄦ浛鎹⑩濇寜閽嵆鍙2銆佹牸寮忔浛鎹㈠鏋滈渶瑕佺獊鍑烘枃妗涓煇浜鐗瑰畾...
绛旓細str[i]=c;//杈撳叆瀛楃涓 i++; } str[i]='\0'; for(i=0;str[i]!='\0';i++) { if(str[i]==' ') { for(j=i+1;str[j]!='\0';j++) { str[j-1]=str[j]; } str[j]='\0'; } else continue; } str[i-2]='\0'; printf("鍘绘帀绌烘牸鍚鐨勫瓧绗︿覆涓:\n"); for(i=0...
绛旓細鍙鏈夊垎鏁板氨鍙互锛屽綋鐒舵湁鐐硅姳鍛楁槸鏈濂戒笉杩囦簡 鐜板湪鍒板鏄獥瀛 澶у涓瀹氳璁ょ湡鍐嶈鐪 璋㈣阿缃戝弸浠殑閲囩撼锛
绛旓細include<stdio.h> include<string.h> void main(){ char ch='\0',s[50]={'\0'},s1[50]={'\0'},s2[50]={'\0'};FILE *fp1,*fp2;fp1=fopen("1.txt","w+");/*鍦ㄦ鏂囦欢涓緭鍏鏂囩珷*/ fp2=fopen("2.txt","w");/*鍦ㄦ鏂囦欢涓緭鍏ヤ慨鏀圭殑鏂囩珷*/ printf("please input the ...
绛旓細绛夋墍鏈夊崟璇嶉兘缁勬垚閾捐〃涔嬪悗銆傚啀娆¢亶鍘嗛摼琛紝鍙cnt涓嶇瓑浜-1鐨勮妭鐐逛釜鏁板氨鏄鏂囩珷鍗曡瘝鐨勬暟閲忥紝涓嶇瓑浜-1鐨刢nt鍊煎氨鏄瘡涓崟璇嶅搴旂殑鍑虹幇娆℃暟 鍒犻櫎鍗曡瘝锛氬彧瑕佹妸瀵瑰簲鑺傜偣鍒犻櫎锛堝鏋滃垹闄ょ殑鏄鍗曡瘝绗竴娆″嚭鐜扮殑鑺傜偣锛屾妸鍏禼nt鍊间繚瀛樺埌鍚屽崟璇嶄笅涓娆″嚭鐜扮殑鑺傜偣涓級銆傛浛鎹㈠崟璇嶏細棣栧厛瑕侀亶鍘嗛摼琛紝纭璇ユ柊鍗曡瘝娌℃湁鍑虹幇...
绛旓細鈶 鍙互c鐨勫嚱鏁皊trtok,鍙互涓娆℃悶瀹;闂鏄繖涓嚱鏁伴渶瑕佹嫹璐濆師瀛楃涓,鍥犱负瀹冭鐮村潖瀛楃涓,濡傛灉鏂囨湰鏂囦欢澶,鏁堢巼灏变細浣; 鑰屼笖鏄嚎绋嬩笉瀹夊叏鐨勩傗憽 鍙互浣跨敤boost::tokenizer, 寰堟柟渚, 涓嶈繃闇瑕佹寜鐓х3鏂圭殑boost搴;鈶 涔熷彲浠ョ敤pcre鐨勬鍒欏簱, 涔熷緢濂界敤;鈶 鑷繁鍐欎竴涓, string, STL鐨; 涓嬮潰鏄敤stringstream鐨勪竴涓...
绛旓細<?php str = ' 娌冲寳锛岀煶瀹跺簞锛屽寳浜,涓婃捣|澶╂触锝滐綔锝滈噸搴嗭綔淇濆畾,,,娌冲崡 ';str = preg_replace("/([ ,锛寍锝淽+)/u", " ", $str);str = ltrim(rtrim($str));print_r($str);?>