gpt4 book ai didi

java - 从用户那里读取一段并替换特定的单词在 java 中

转载 作者:行者123 更新时间:2023-11-29 08:33:20 25 4
gpt4 key购买 nike

我们如何使用 Java 编写一个程序来读取用户的一段话并将 vector 中提到的特定单词替换为以下格式,即

例如单词Happy被简化为H****

我们将不胜感激。

import java.io.*;
import java.util.*;

class replaceString {

public static String putStars(String str) {
char first_char = str.charAt(0);

String ans = new String();
ans = String.valueOf(first_char);

for(int i = 1;i < str.length(); ++i ) {
ans = ans + "*";
}
return ans;
}

public static String replaceWords(String str, Vector<String> v1) {
String[] words = str.split("\\W+"); //split across all types of punctuation

String ansString = new String();

for(String to_be_replaced : words) {
boolean found = false;

for(String to_replace_with : v1) {

if(to_be_replaced.equals(to_replace_with)) {
//System.out.println("in");
ansString = ansString +putStars(to_be_replaced) + " ";
found = true;

}
}
if(found == false) {
ansString = ansString + to_be_replaced + " ";
}

}
return ansString;
}

public static String replaceWords1(String str, Vector<String> v1) {

for(String currStr : v1) {
str.replace(str, );
}

return ansString;
}


public static void main(String args[])throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the paragraph that you would like to edit ");

String s = br.readLine();

// Let us assume some strings in our very own vector

Vector<String> v1 = new Vector<String>();

v1.addElement("Hello");
v1.addElement("Hi");
v1.addElement("Heya");
v1.addElement("Howdy");
v1.addElement("Howu");

String ans = replaceWords(s, v1);

System.out.println("Paragraph after replacement becomes\n\n"+ ans);
}
}

这是我当前的代码,但它不能正常工作

最佳答案

可能还有其他可能性,但这是我根据 this answer 做的一个例子:

  1. 我们需要/想要匹配的所有单词,并将它们存储在一个数组中:

    String [] words = {"appy", "eya", "dy"};
  2. (可选)如果您真的需要一个Vector,我建议创建一个List (ArrayList),我们可以这样做:

    List <String> wordsToReplace = Arrays.asList(words);

    否则直接修改下一步接收数组的方法即可...

  3. 我们创建一个函数来接收此 List 和我们要检查的短语,并返回新的 String 以及其中替换的文本

所以,我们的整个代码最终是这样的:

import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WordReplacer {
public static void main(String[] args) {
String [] words = {"appy", "eya", "dy"};

List <String> wordsToReplace = Arrays.asList(words);

System.out.println(replaceWords("Happy", wordsToReplace));
System.out.println(replaceWords("Heya", wordsToReplace));
System.out.println(replaceWords("Howdy?", wordsToReplace));
System.out.println(replaceWords("Howdy? My friend lives in Pompeya and every time I see her I say \"Heya\" to her, she is very happy", wordsToReplace));
}

private static String replaceWords(String word, List <String> wordsToReplace) {
for (String s : wordsToReplace) {
Pattern p = Pattern.compile(s, Pattern.CASE_INSENSITIVE); //We create a pattern that matches each word in our list. (1)
Matcher m = p.matcher(word); //We actually check for each match against our phrase
StringBuilder sb = new StringBuilder();
if (m.find()) { //If there was a match, we're going to replace each character for an '*' (2)
for (int i = 0; i < s.length(); i++) {
sb.append("*");
}
}

word = m.replaceAll(sb.toString()); //We replace each match with '*' (3)
}

return word; //We return the modified word
}
}

我将以更好、更简单的方式解释每条评论 (1)、(2)、(3) 的作用:

  • (1) 如链接答案所示,他们使用 \b 正则表达式命令来匹配整个单词,但在这种情况下我们使用它来匹配部分单词,而不是整个单词,所以我们在这里不需要它...

  • (2) 仅当我们找到匹配时,我们才用 * 字符填充 StringBuilder...如果我们没有这样做,我们将得到: H* 而不是 H**** 对于 Happy 字的情况,这样我们确保我们得到正确的数量* 用于 List 中的每个单词。

  • (3) 我们替换 StringBuilder 中的 * 总数的匹配项,以便我们得到正确的输出。

上面的程序产生以下输出:

H****
H***
How**?
How**? My friend lives in Pomp*** and every time I see her I say "H***" to her, she is very h****

关于java - 从用户那里读取一段并替换特定的单词在 java 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46039958/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com