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

Spring - Extending classes with hibernate and spring security
Spring - inject contents of file from classpath into annotated member
javax.websocket client android app simple example
I need to use java 8 filter to filter my list of custom objects in efficient manner
Google Speech API Java Client does not work on my computer
My program gives me error ArrayIndexOutOfBoundsException [duplicate]
http component can't connect to proxy
Clone a Set including real type
how can i display a message dialog box in java
JTextArea admin password control
Playing Youtube videos with the Youtube API and an ExpandableListView - ANDROID STUDIO
maven jar plugin with specific filename not work
Need help, trying to read a file and fill an array of objects with their parameters in java
BufferedReader and BufferedWriter with Socket
Is String a immutable class in JAVA, So how to use this feature is multi-Thread programing?
Loading drawable into Fragment

Categories

HOME
twitter
listview
gerrit
tizen
routes
react-redux
yarn
infragistics
google-project-tango
append
add
iggrid
dax
hex-editors
reactive-cocoa
beyondcompare
textfield
introduction
riot.js
css-animations
php-openssl
sparse-matrix
fish
intel-pin
rst2pdf
social-media
libssl
c++-amp
primitive
google-rich-snippets
preg-match-all
semantic-versioning
exuberant-ctags
create-table
google-qpx-express-api
gtrendsr
http-get
commit
appfabric
simplexml
bitbucket-pipelines
swisscomdev
elasticsearch-plugin
bootstrapper
user-controls
squib
hp-ux
avro4s
google-closure
botbuilder
ibpy
spring-security-kerberos
bootstrap-dialog
babel-core
auto-update
upstart
pubmed
logparser
paxos
ionicons
colorama
nsarray
r-forge
ifs
imanage
bind9
gridpane
intrusion-detection
login-control
etsy
disque
dlna
yaws
iad
wso2cloud
ora-00900
google-places
dukescript
twirl
pretty-print
c++03
directoryservices
system.reflection
facebook-graph-api-v2.4
tablelayout
relocation
xojo
castle
google-reader
p4java
xceed-datagrid
message-driven-bean
typo3-neos
jsctypes
jquery-mobile-dialog
spring-validator
lcs
quickdialog
broken-links
venn-diagram
reddot
subscript
genshi
table-footer
google-friend-connect
sudzc
dmx512
sproutcore-2
gin
modelstate
kdbg
putchar
mdac
signal-handling

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App