導航:首頁 > 編程語言 > java文本替換

java文本替換

發布時間:2023-03-04 20:51:50

❶ 用java編一個文本漢字替換程序

package com.;

import java.io.*;

public class ReplaceChinese {
public static void main(String[] args) {
String filePath = "F:\\workspace\\onlineChat\\src\\com\\\\ReplaceChinese.java";
File file = new File(filePath);
if (!file.exists()) {
System.out.println("文件不存在!");
return;
}
BufferedReader reader = null;
String result = "";// 用於存修改後的文字
String lineString = "";
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// 一次讀入一行,直到讀入null為文件結束
while ((tempString = reader.readLine()) != null) {
// 顯示行號
for (int i = 0; i < tempString.length(); i++) {
if (tempString.substring(i, i + 1).matches(
"[\u4e00-\u9fa5]")) {
lineString += "[您要替換的代碼]";
} else {
lineString += tempString.substring(i, i + 1);
}
}
result += lineString + "\n";
lineString = "";
line++;
}
reader.close();
BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream("F:\\workspace\\onlineChat\\src\\com\\\\ReplaceChinese1.java")));
bw.write(result);
bw.flush();
bw.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
}
由於時間緊迫,只把功能實現了,優化你自己做做,能夠成功運行

❷ java怎樣將字元串中的字母替換掉

一:思路:

使用java方法replaceAll();通過正則表達式匹配替換掉所有的字母。

二:代碼如下(可直接復制出來運行,在控制台中查看效果):

publicstaticvoidmain(String[]args){
Stringstr="abc123123成你懂嗎bxcxsaf";
//通過正則表達式替換掉所有的字母
StringstrNew=str.replaceAll("[a-zA-Z]","");
System.out.println(strNew);
}

運行結果如下:

三:擴展(正則表達式)

正則表達式使用單個字元串來描述、匹配一系列符合某個句法規則的字元串。在很多情況下,通常被用來,檢索和替換符合某個規則的文本。

PS:有興趣可以深入研究一下正則表達式的語法,及規則。

❸ Java 字元串替換

importjava.util.regex.*;
publicclassRepTest{
publicstaticvoidmain(String[]args){
Stringsrc=">=,<=,=,>=,<=,=,>=,<=,=,>=,<=,=,>=,<=,=,>=,<=,=,";
System.out.println("原串:"+src);
Matcherma=Pattern.compile("[^><]=").matcher(src);
while(ma.find()){
src=src.replaceAll(ma.group(),"");
}
System.out.println("替換:"+src);
//其實還有一個思路,你可以拿逗號切成數組,然後對數組元素進行判斷,拿=號切也可以!
}
}

❹ java 替換文件內容

代碼如下:
/***
* 方法:
* @Title: replaceContentToFile
* @Description: TODO
* @param @param path 文件
* @param @param str 開始刪除的字元
* @param @param con 追加的文本
* @return void 返回類型
* @throws
*/
public static void replaceContentToFile(String path, String str ,String con){
try {
FileReader read = new FileReader(path);
BufferedReader br = new BufferedReader(read);
StringBuilder content = new StringBuilder();

while(br.ready() != false){
content.append(br.readLine());
content.append("\r\n");
}
System.out.println(content.toString());
int dex = content.indexOf(str);
if( dex != -1){
System.out.println(content.substring(dex, content.length()));
content.delete(dex, content.length());
}
content.append(con);
br.close();
read.close();
FileOutputStream fs = new FileOutputStream(path);
fs.write(content.toString().getBytes());
fs.close();

} catch (FileNotFoundException e) {
e.printStackTrace();

} catch (IOException e){
e.printStackTrace();

}

}

❺ java如何實現替換指定位置的指定字元串的功能

可以使用StringBuffer定義字元串,之後使用replace方法替換指定位置的字元串為指定的字元串內容,如下代碼:
public
class
Demo1
{
public
static
void
main(String[]
args)
{
StringBuffer
buffer
=
new
StringBuffer("123456");
System.out.println(buffer.toString());//輸出123456
buffer.replace(0,
1,
"a");
System.out.println(buffer.toString());//輸出a23456
}
}
這里簡單介紹一下replace方法的使用,replace方法一共有三個參數,第一個參數是指定要替換的字元串的開始位置,第二個參數是指定要替換的字元串的結束位置(注意這里的結束位置不包括本身),第三個參數是指定想將字元串替換成什麼內容。
如:原字元串內容為"123456",現在調用replace(0,
2,
"abc"),原字元串變為"abc3456"

❻ java如何替換文本中所有的字元串ab,但abc中的ab不變

建議使用正則匹配

替換文本中所有的字元串ab,但abc中的ab不變

ab(?!c)

就是說如果現在要替換asdfgh,如果有asdfghjkl,這個地方不換,只有asdfgh前後不是英文字母才換

如果是獨立單詞的話:

asdfgh

如果是匹配前後不是英文字母的話:

[^a-zA-Z](asdfgh)[^a-zA-Z]?
閱讀全文

與java文本替換相關的資料

熱點內容
ios支持的解壓縮格式 瀏覽:703
平安經營貸結清後如何解壓 瀏覽:938
蘋果系統的解壓縮軟體 瀏覽:856
python火鍋店運營分析 瀏覽:985
c語言編譯器手機在線 瀏覽:848
戰艦世界什麼伺服器地址 瀏覽:550
windowsphone解壓縮 瀏覽:646
android工程目錄結構 瀏覽:137
pdf文檔是反的 瀏覽:528
javaobject比較 瀏覽:867
安卓如何設置微信屏幕鎖 瀏覽:189
本溪雲伺服器 瀏覽:375
玩機技巧華為app如何了解純凈模式 瀏覽:905
換演算法則數不變 瀏覽:719
java工作流activiti 瀏覽:788
單片機自動門程序 瀏覽:423
java培訓長沙 瀏覽:494
程序員生存現狀 瀏覽:588
光環游戲安裝器在哪個文件夾 瀏覽:654
公眾號圖片被壓縮 瀏覽:291