【问题标题】:replace a particular word in string替换字符串中的特定单词
【发布时间】:2017-05-11 12:26:02
【问题描述】:

假设这些是一些 URL,

http://www.example.com/conditions/redirecting/home/ovc-20197858
http://www.example.com/conditions/gotos/definition/sys/ovc-20197858

现在我想用 place-for-you 替换 url 的单词 home 如果 url 包含 home 和 definition如果 url 包含 定义,则放置。为此我写了一个代码,但它没有改变网址。

String newurl = "";
String url = "http://www.example.com/conditions/redirecting/home/ovc-20197858";
String home = "home";
String definition = "definition";
             boolean check = url.matches("home");
// already tried boolean check = url.contains("home");

             if(check == true)
             {
             newurl = url.replace(home,"place-for-you"); 
             }
             else
             {
          newurl = url.replace(definition,"place"); 
             }

【问题讨论】:

    标签: java string replace


    【解决方案1】:

    String#matches() 是您与regex 一起使用的东西

    你需要使用 contains 代替:

    boolean check = url.contains("home");
    

    示例:

    if (url.contains("home")) {
            newurl = url.replace(home, "place-for-you");
    

    编辑:

    【讨论】:

    • 我不知道为什么当我应用相同但我复制粘贴您的代码时它不起作用,它开始工作......!谢谢:)
    • 但它不适用于 if (url.contains("definition")) { newurl = url.replace(definition, "place"); }
    • 我明白了,但是你需要另一个字符串吗??
    • 如果在第一次之后再做一次:)
    • 这就是我正在做的事情
    【解决方案2】:

    无需检查您的字符串是否包含该字符串replace 会这样做:

    url = url.replace("home", "place-for-you");
    url = url.replace("definition", "place");
    

    因为替换会检查输入是否包含这个字符串十替换否则它不会替换它

    【讨论】:

      【解决方案3】:

      用户可以使用此方法检查 Home 是否存在。

      public class Algo {
          public static void main(String[] args) {
              String newurl = "";
              String url = "http://www.example.com/conditions/redirecting/home/ovc-20197858";
              String home = "home";
              String definition = "definition";
              boolean check = url.contains("home");
      
              if (check == true) {
                  newurl = url.replace(home, "place-for-you");
              } else {
                  newurl = url.replace(definition, "place");
              }
              System.out.print(newurl);
          }
      
      }
      

      【讨论】:

        【解决方案4】:

        如果您查看matches 方法,您会发现您传递给它的字符串应该是一个正则表达式。在您的情况下,您传递的是 boolean check = url.matches("home"); 您的 URL 与 "home" 不同,因此它将始终返回 false。如果您想使用正则表达式执行此操作,则需要将此行更改为 boolean check = url.matches(".*home.*"); 在正则表达式中,.* 表示任何字符 0 次或多次。

        如果您只想检查字符串 "home" 是否存在,我建议将整行更改为 boolean check = url.contains("home");

        它确实有效 - 自己运行以下 Junit 测试:

            @Test
            public void test() {
                String newurl = "";
                String url = "http://www.example.com/conditions/redirecting/home/ovc-20197858";
                String home = "home";
                String definition = "definition";
                boolean check = url.matches(".*home.*");
        
                if(check == true)
                {
                    newurl = url.replace(home,"place-for-you");
                }
                else
                {
                    newurl = url.replace(definition,"place");
                }
        
                assertEquals(newurl, "http://www.example.com/conditions/redirecting/place-for-you/ovc-20197858");
            }
        

        【讨论】:

        • 我已经这样做了,但它不起作用我认为它不是关于在字符串中查找单词,而是关于查找模式,因为它是一个 URL,它是一个完整的字符串
        【解决方案5】:

        这是工作示例,

        import java.util.*;
        public class demo
        {
            public static void main(String args[])
            {
            String newurl = "";
            String url = "http://www.example.com/conditions/redirecting/home/ovc-20197858";
            String home = "home";
            String definition = "definition";
                     boolean check = url.contains("home");
        
                     if(check == true)
                newurl = url.replace(home,"place-for-you"); 
                     else
                newurl = url.replace(definition,"place"); 
        
            System.out.println("Old URL : " + url);
            System.out.println("New URL : " + newurl);
            }
        }
        

        【讨论】:

          【解决方案6】:

          请记住,String 是不可变的,因此您无法更改它。 所以最好的方法是使用 StringBuilder 代替

          String newurl = "";
                  StringBuilder url = new StringBuilder("http://www.example.com/conditions/redirecting/home/ovc-20197858");
                  String home = "home";
                  String definition = "definition";
                  if (url.indexOf("home") != -1) {
                      url.replace(url.indexOf("home"), url.indexOf("home") + 4, "place-for-you");
                  } else {
                      url.replace(url.indexOf("definition"), url.indexOf("definition") + 10, "place");
                  }
          

          【讨论】:

            猜你喜欢
            • 2012-09-14
            • 2021-10-02
            • 1970-01-01
            • 2021-06-30
            • 2021-08-20
            • 2013-09-06
            • 1970-01-01
            • 2011-04-13
            • 2015-03-03
            相关资源
            最近更新 更多