Solved

String manipulation

Posted on 2003-11-17
13
3,452 Views
Last Modified: 2010-03-31
Say i have a string representing a url such as:

String myUrl = "http://kpldb:8083/hartlepool/communityPortalHome/content/councilGateway/?view=Standard$a=5441"

What it the best and most cost effective way of taking the string and changing 'view=Standard' to 'view=TextOnly', bearing in mind the string will not always be the same length and $a=5441 will not always be on the ene
0
Comment
Question by:danBosh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +2
13 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9765222
Something like:

            String myUrl = "http://kpldb:8083/hartlepool/communityPortalHome/content/councilGateway/?view=Standard$a=5441";
            final String TARGET = "view=Standard";
            final String NEW = "view=TextOnly";
            int start = -1;

            if ((start = myUrl.indexOf(TARGET)) > -1) {
                  StringBuffer sb = new StringBuffer(myUrl);
                  sb.delete(start, start + TARGET.length());
                  sb.insert(start, NEW);
                  System.out.println(sb.toString());
            }
0
 
LVL 20

Accepted Solution

by:
Venabili earned 40 total points
ID: 9766451
What JDK do you use? If 1.4 you can use replace.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9767353
Venabili is almost right ;-)  It's replaceAll(), not replace() ;-)

If you are using 1.4, you could do the following:

        String orig = "http://kpldb:8083/hartlepool/communityPortalHome/content/councilGateway/?view=Standard$a=5441";
        String replaced = orig.replaceAll("view=Standard", "view=TextOnly");
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Expert Comment

by:Venabili
ID: 9767413
:) Agree
0
 
LVL 92

Expert Comment

by:objects
ID: 9768590
Possible problems with those approaches is if the String contains the text 'view=Standard' elsewhere in the url. If it is possible that this is the case then you'll need to parse the URL to perform the replacement. Let me know if this is the case and I'll show you a bullet proff approach.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9770160
>>
Possible problems with those approaches is if the String contains the text 'view=Standard' elsewhere in the url.
>>

No with my approach ;-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9770162
No=Not ;-)
0
 

Author Comment

by:danBosh
ID: 9772200
sorry CEHJ i ment to accept your answer, i made a school boy error
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9772387
Don't worry. Repetition is not an issue anyway:

1. it can't be included before the query string
2. if it occurs more than once in the query string it'll be ignored by the server after the first occurrence anyway
0
 
LVL 92

Expert Comment

by:objects
ID: 9774351
> No with my approach ;-)

Yes it can have problems.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9777337
replaceFirst() is another alternative (aside from the fact that the point is now moot anyway ;-))
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9777682
>>now moot

now academic surely? ;-)
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9777748
Indeed :-P

;-)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

697 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question