替换字符串中的特定单词

时间:2021-03-16 19:22:09

Suppose these are some URLs,

假设这些是一些URL,

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

now what i want to replace the url's word home with place-for-you if url consist home and definition with place if urlcontains definition. For this i wrote a code but its not changing the url.

现在,如果url包含在家,并且如果urlcontains定义,则将地址定义为替换url的单词home for place for for you。为此,我写了一个代码,但它不改变网址。

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 个解决方案

#1


2  

String#matches() is something that you use with regex

String#matches()是你用regex的东西

you need to use contains instead:

你需要使用包含:

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

example:

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

Edit:替换字符串中的特定单词

#2


0  

No need to check if your String contain that string or not, replace will do that :

无需检查您的String是否包含该字符串,replace将执行此操作:

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

because replace will check if the input contain this string ten replace else it will not replace it

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

#3


0  

Use can use this method to check if Home exist or not.

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


0  

If you have a look at the matches method you will see that the string you pass it's supposed to be a regular expression. In your case you are passing boolean check = url.matches("home"); Your URL is different than "home" so it will always return false. If you want to do this with regular expression you need to change this line to boolean check = url.matches(".*home.*"); In regular expression .* means any character 0 or more times.

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

If you want to just check if the String "home" exists I'd suggest changing the whole line to boolean check = url.contains("home");

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

It definitely works - run the following Junit test for yourself:

它绝对有效 - 为自己运行以下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");
    }

#5


0  

Here is the working example,

这是工作示例,

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


0  

Kindly keep in mind that String is immutable, so you can't change it. So best approach would be using StringBuilder instead like

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

#1


2  

String#matches() is something that you use with regex

String#matches()是你用regex的东西

you need to use contains instead:

你需要使用包含:

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

example:

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

Edit:替换字符串中的特定单词

#2


0  

No need to check if your String contain that string or not, replace will do that :

无需检查您的String是否包含该字符串,replace将执行此操作:

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

because replace will check if the input contain this string ten replace else it will not replace it

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

#3


0  

Use can use this method to check if Home exist or not.

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


0  

If you have a look at the matches method you will see that the string you pass it's supposed to be a regular expression. In your case you are passing boolean check = url.matches("home"); Your URL is different than "home" so it will always return false. If you want to do this with regular expression you need to change this line to boolean check = url.matches(".*home.*"); In regular expression .* means any character 0 or more times.

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

If you want to just check if the String "home" exists I'd suggest changing the whole line to boolean check = url.contains("home");

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

It definitely works - run the following Junit test for yourself:

它绝对有效 - 为自己运行以下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");
    }

#5


0  

Here is the working example,

这是工作示例,

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


0  

Kindly keep in mind that String is immutable, so you can't change it. So best approach would be using StringBuilder instead like

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