Aman
Aman

Reputation: 838

replace a particular word in string

Suppose these are some URLs,

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.

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

Upvotes: 1

Views: 2193

Answers (6)

Afridi
Afridi

Reputation: 6932

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

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

Upvotes: 0

Unknown_Coder
Unknown_Coder

Reputation: 774

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

Upvotes: 0

Anton Belev
Anton Belev

Reputation: 13523

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.

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

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

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

Upvotes: 0

gauravsngarg
gauravsngarg

Reputation: 666

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

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

}

Upvotes: 0

Youcef LAIDANI
Youcef LAIDANI

Reputation: 59950

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

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

Upvotes: 0

String#matches() is something that you use with 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:enter image description here

Upvotes: 2

Related Questions