简体   繁体   English

替换Java中一部分字符串

[英]replace part of a string in java

I'm trying to write a function which takes in a string such as String code = "<div> style="width: 3%" </div>" 我正在尝试编写一个接受字符串的函数,例如String code = "<div> style="width: 3%" </div>"

I would like to replace the 3% to another number (can be more than 2 digits) so 400% etc. 我想将3%替换为另一个数字(可以超过2位数字),所以替换为400%等。

I'm currently getting the charAt(21) but this is only the index of 3, so if I had 20% in that place then my code would not work. 我目前正在使用charAt(21),但这只是3的索引,所以如果我在那个地方有20%的代码,则我的代码将无法工作。

Is there another way to replace the place where the % is stored. 还有另一种方法可以替换%的存储位置。 (also doing this by not knowing what number the current % is) (也可以通过不知道当前百分比是多少来进行此操作)

You can do this using a regular expression, for example: 您可以使用正则表达式来执行此操作,例如:

String code = "<div> style=\"width: 3%\" </div>"
String replaced = code.replaceFirst("width: \\d+", "width: 400")

To extract the value in the existing string: 要提取现有字符串中的值:

Pattern pattern =  Pattern.compile("width: (\\d+?)%");
Matcher matcher = pattern.matcher(code);
matcher.find()
matcher.group(1)//Gives 3

The best approach would be using HTML parser. 最好的方法是使用HTML解析器。 But if your string will always be simple, you can use replaceAll which takes a regex: 但是,如果您的字符串始终很简单,则可以使用带有正则表达式的replaceAll

String code = "<div> style=\"width: 3%\" </div>";
String res = code.replaceAll(yourRegex, replacement);

I will leave the full solution for you, but will give you few hints: 我将为您提供完整的解决方案,但会给您一些提示:

  • Regex tutorial 正则表达式教程
  • String#replaceAll
  • \\d+ will match digits, so width:\\s+\\d+ will match "width" followed by space(s) and then digit(s) \\d+将匹配数字,因此width:\\s+\\d+将匹配“ width”,后跟空格,然后是数字
  • Use parenthesis to group the caught result from the regex, for example width:\\s+(\\d+) will catch the result of the digits (group) 使用括号将正则表达式捕获的结果分组,例如width:\\s+(\\d+)将捕获数字的结果(分组)

If your String it's: 如果您的字符串是:

String code = "<div> style="width: 3%" </div>"

You can find the position in which the % it's stored, like this: 您可以找到%存储的位置,如下所示:

int position = code.indexOf("%");

And suppossing that you have your number stored in a String: 并假设您将数字存储在字符串中:

String number = "2";

Note: It doesn't matter if it is 2 or 22 or 222 . 注意:2还是22222都没有关系。

You can get the total String with substring function: 您可以使用substring函数获取总String

String totalString = code.substring(0,position - 1) + number + code.substring(position+1, code.length());

And now you can print it normally: 现在您可以正常打印了:

System.out.println(totalString);

I expect it will works for you! 我希望它将为您服务!

Use String#replaceFirst or String#replaceAll method: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html 使用String#replaceFirst或String#replaceAll方法: http ://docs.oracle.com/javase/7/docs/api/java/lang/String.html

code = code.replaceFirst("width: ?(.*)%", "width: 400%");

Of course adapt the regex to your requirement 当然可以使正则表达式适应您的要求

您可以使用HTML解析器,例如Jsoup。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM