「java替换汉字」java替换字符串的几种方法

博主:adminadmin 2023-03-21 15:10:09 505

本篇文章给大家谈谈java替换汉字,以及java替换字符串的几种方法对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

大神们,如何在java程序中把某个字段显示的英文字母替换为特定的汉字???在jsp页面中能替换吗???

c:if test="${e.college_id}.eq(总计那一列的ID)"

td总计/td

/c: 把这段判断放去forEach里,循环一次就判断一次,成立就替换,不成立就按原来的步骤执行

java 利用for语句和if语句把字符串中的一个指定字母换成指定汉字

这就是正则表达式要解决的问题。

java.util.regex;

public final class Pattern extends Object implements Serializable

正则表达式的编译表示形式。

public final class Matcher extends Object implements MatchResult

通过解释 Pattern 对 character sequence 执行匹配操作的引擎。

通过调用模式的 matcher 方法从模式创建匹配器。创建匹配器后,可以使用它执行三种不同的匹配操作:

matches 方法尝试将整个输入序列与该模式匹配。

lookingAt 尝试将输入序列从头开始与该模式匹配。

find 方法扫描输入序列以查找与该模式匹配的下一个子序列。

每个方法都返回一个表示成功或失败的布尔值。通过查询匹配器的状态可以获取关于成功匹配的更多信息。

public String replaceAll(String replacement)替换模式与给定替换字符串相匹配的输入序列的每个子序列。

实例:

Pattern p = Pattern.compile("A");

Matcher m = p.matcher("ddfefaAgdcd");

m.replaceAll("艾");

用java编一个文本汉字替换程序

package com.baidu;

import java.io.*;

public class ReplaceChinese {

public static void main(String[] args) {

String filePath = "F:\\workspace\\onlineChat\\src\\com\\baidu\\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\\baidu\\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如何将两个汉字替换

public String replace(String value,char a,char b){

char[] cs=value.toCharArray();

for(int i=0;ics.length;i++){

if(cs[i]==a){

cs[i]=b;

}else if(cs[i]==b){

cs[i]=a;

}

}

return new String(cs);

}

以上方法仅用于单个字的替换,如果用于字符串替换的话需要用indexOf方法去遍历整个字符串了。

java替换汉字的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java替换字符串的几种方法、java替换汉字的信息别忘了在本站进行查找喔。