java


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

Related Links

Java application properties file
Java aware merge command
Java Clip (Sound / Audio) Memory Leak after closing with close()
How to create bitmap from integer array in C / OpenGL
Java: Invalid keystore format, when generated through code
Java Stanford NLP: Find word frequency?
Listening for new child components in Swing
mapping a postgres array with hibernate
How do I edit a XML node in a file object, using Java
How can use Struts 2.0, Spring and Hibernate in my framework
How can I iterate over a map of <String, POJO>?
How could I know whether org.eclipse.swt.browser.Browser.setText(html) renderred html completed
struts 1.2 with Ajax
Where to place main game loop in android game
JPA: Eclipselink does not persist bi-directional relationships in database
Are there any API to integrate Microsoft exchange server with Java application for Task synchronization?

Categories

HOME
google-chrome-extension
bluetooth
omnet++
iterator
hp-exstream
sqlite-net-extensions
setup-deployment
ojdbc
constraint-programming
paradox
add
slick-slider
fancybox-3
spring-xd
postgres-xl
task
fortumo
quickfix
serverless-framework
libtiff
progressive-web-apps
grails3
autoconf
java-3d
scriptcs
graphicsmagick
fish
semantic-analysis
facebook-access-token
wpfdatagrid
dbclient
google-sites-2016
kendo-ui-grid
framemaker
subset-sum
mmenu
revolution-slider
suricata
objectlistview
tasker
mapzen
businessworks
qcombobox
btrace
jedis
hls.js
dropbox-php
rdfs
media-player
pillow
pdfclown
intrusion-detection
google-web-starter-kit
django-debug-toolbar
livequery
energy
atk4
content-length
kcachegrind
ionic
appfabric-cache
dundas
skos
website-monitoring
gulp-less
tld
jquery-layout
centos5
dbconnection
gridcontrol
seed
twrequest
tridion-worldserver
joyent
android-sdk-2.1
datareader
fluent-interface
nhibernate.search
preference
firefox-5
anti-piracy
noscript
project-hosting
mediarss
geneva-server
3gp

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App