替换字符串中的特定单词

时间:2017-05-11 12:26:03

标签: java string replace

假设这些是一些网址,

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

现在我想用替换为替换网址主页,如果网址为定义 如果urlcontains 定义,则。为此,我写了一个代码,但它不改变网址。

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"); 
             }

6 个答案:

答案 0 :(得分:2)

Splitter-AggregateTaskregex

一起使用

你需要使用contains:

String#matches()

示例:

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

编辑:enter image description here

答案 1 :(得分:0)

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

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

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

答案 2 :(得分:0)

如果您查看matches方法,您会看到传递它的字符串应该是正则表达式。在您的情况下,您正在传递boolean check = url.matches("home");您的网址与"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");
    }

答案 3 :(得分:0)

请记住,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");
        }

答案 4 :(得分:0)

使用此方法可以检查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);
    }

}

答案 5 :(得分:0)

以下是工作示例

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);
    }
}