Tag: 替换

当替换文本与搜索文本重叠时,用Java替换多个子字符串

假设您有以下字符串: cat dog fish dog fish cat 你想用dogs取代所有的cats ,用fish取代所有的dogs ,用cats取代所有的fish 。 直观地说,预期结果: dog fish cat fish cat dog 如果你尝试使用replaceAll()循环的明显解决方案,你得到: (原创) cat dog fish dog fish cat (猫 – >狗) dog dog fish dog fish dog (狗 – >鱼) fish fish fish fish fish fish (鱼 – >猫) cat cat cat cat cat cat 显然,这不是预期的结果。 那么最简单的方法是什么? […]

Java,无限循环的替代?

我正在制作一个通过arrays显示细胞生长的程序。 当我按下开始按钮时,我已经得到它,数组在一段时间(真实){}循环中每10秒更新一次。 问题是我希望能够通过按下暂停按钮来停止循环,但是在循环中,它不会让我使用任何控件。 我需要在orer中使用除了无限循环之外的其他东西来刷新帧。 我是一个新手,但我现在在一个java类。 所以我掌握了一些语言。

如何使用replace(char,char)来替换所有字符b的实例

我如何使用replace(char,char)来替换所有字符“b”的实例。 例如: Hambbburger to Hamurger 编辑:有一个约束,我可能只使用1.4.2,意味着没有重载的超载版本!

正则表达式中的特殊字符

public static final String specialChars1= “\\W\\S”; String str2 = str1.replaceAll(specialChars1, “”).replace(” “, “+”); public static final String specialChars2 = “`~!@#$%^&*()_+[]\\;\’,./{}|:\”?”; String str2 = str1.replaceAll(specialChars2, “”).replace(” “, “+”); 无论str1是什么,我都希望删除除字母和数字之外的所有字符,并将空格替换为加号( + )。 我的问题是,如果我使用specialChar1 ,它不会删除一些字符,如; , ‘ , ” ,如果我使用specialChar2它会给我一个错误: java.util.regex.PatternSyntaxException: Syntax error U_REGEX_MISSING_CLOSE_BRACKET near index 32: 怎么能实现这个目标? 我搜索过但找不到完美的解决方案。

使用Java静态方法中的“today”和“yesterday”字符串简化日期对象的替换

我有以下方法,我想更短或更快,如果没有别的。 欢迎所有评论: Bellow方法接受一个日期对象,形成它(“EEE hh:mma MMM d,yyyy”),然后确定日期是今天还是昨天,如果是,则返回“(昨天|今天)hh:mma “格式化的字符串。 public static String formatToYesterdayOrToday(String date) { SimpleDateFormat sdf = new SimpleDateFormat(“EEE hh:mma MMM d, yyyy”); Date in = null; try { in = sdf.parse(date); } catch (ParseException e) { log.debug(“Date parsing error:”, e); } Calendar x = Calendar.getInstance(); x.setTime(in); String hour = Integer.toString(x.get(Calendar.HOUR)); String minute = Integer.toString(x.get(Calendar.MINUTE)); String […]

从字符串中删除特定单词

我正在尝试使用函数replace()或replaceAll()删除某个字符串中的特定单词,但这些单词会删除所有出现的单词,即使它是另一个单词的一部分! 例: String content = “is not like is, but mistakes are common”; content = content.replace(“is”, “”); 输出: “not like , but mtakes are common” 期望的输出: “not like , but mistakes are common” 我怎样才能只替换字符串中的整个单词?

字符串替换不起作用,因为我认为它应该

public static boolean passwordConfirmed() { String attempt = JOptionPane.showInputDialog(“Password: “); FileReader fstream = null; String password = “”; try { fstream = new FileReader(“pass.txt”); BufferedReader in = new BufferedReader(fstream); password = in.readLine(); password.replace(“Password: “, ” “); System.out.println(password); } catch (IOException e) { e.printStackTrace(); } if (attempt.equals(password)) { System.out.print(“True”); return true; } else System.out.println(“false”); return false; […]

Java:用可点击的HTML链接替换文本URL

我正在尝试将包含某些URL的String替换为与浏览器兼容的链接URL。 我的初始String看起来像这样: “hello, i’m some text with an url like http://www.the-url.com/ and I need to have an hypertext link !” 我想得到的是一个字符串看起来像: “hello, i’m some text with an url like http://www.the-url.com/ and I need to have an hypertext link !” 我可以使用以下代码行捕获URL: String withUrlString = myString.replaceAll(“.*://[^[:space:]]+[[:alnum:]/]”, “HereWasAnURL”); 也许regexp表达式需要一些修正,但它工作正常,需要在更长的时间内进行测试。 所以问题是如何保持regexp捕获的表达式,只需添加创建链接所需的内容:catched string 提前感谢您的关注和回复!

用单反斜杠替换双反斜杠

我有一个字符串“\\ u003c”,它属于UTF-8字符集。 由于存在双反斜杠,我无法将其解码为unicode。 我如何从“\\ u003c”获得“\ u003c”? 我正在使用java。 我试过了, myString.replace(“\\\\”, “\\”); 但无法实现我想要的。 这是我的代码, String myString = FileUtils.readFileToString(file); String a = myString.replace(“\\\\”, “\\”); byte[] utf8 = a.getBytes(); // Convert from UTF-8 to Unicode a = new String(utf8, “UTF-8”); System.out.println(“Converted string is:”+a); 和文件的内容是 \ u003c

是否有正则表达式方法用另一组替换一组字符(如shell tr​​命令)?

shell tr命令支持用另一组替换一组字符。 例如, echo hello | tr [az] [AZ] echo hello | tr [az] [AZ]将hello为HELLO 。 但是,在java中,我必须单独替换每个字符,如下所示 “10 Dogs Are Racing” .replaceAll (“0”, “0”) .replaceAll (“1”, “1”) .replaceAll (“2”, “2”) // … .replaceAll (“9”, “9”) .replaceAll (“A”, “A”) // … ; apache-commons-lang库提供了一个方便的replaceChars方法来进行这种替换。 // half-width to full-width System.out.println ( org.apache.commons.lang.StringUtils.replaceChars ( “10 Dogs Are Racing”, “0123456789ABCDEFEGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz”, […]