Solved

Java Replace string

Posted on 2012-04-07
9
453 Views
Last Modified: 2012-04-07
Experts:

I've been trying to figure out how to use the "replace" method to replace a part of a string with a new part.  Nothing I've read makes any sense to me.  Maybe because I'm tired.  I don't know.  But this is what I'm doing and it's not working:

            String newPath = path.replace("\\\\en_US\\\\", "\\\\ko_KR\\\\");

Please help me get the part of the string replaced.  It is a path to a new location on the hard drive.

path = "C:\workspace\FlexApps\locale\en_US\resources.properties"

The new path should be:

path = "C:\workspace\FlexApps\locale\ko_KR\resources.properties"

Thank you in advance,
P
0
Comment
Question by:pdlarue
  • 6
  • 2
9 Comments
 
LVL 23

Expert Comment

by:basicinstinct
ID: 37820392
String newPath = path.replace("\\en_US\\", "ko_KR");
0
 
LVL 23

Accepted Solution

by:
basicinstinct earned 500 total points
ID: 37820398
sorry this:
String newPath = path.replace("\\en_US\\", "\\ko_KR\\");
0
 
LVL 17

Expert Comment

by:Anuroopsundd
ID: 37820481
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:pdlarue
ID: 37820523
OK.  My mistake.  I actually did have essentially the same code:

String newPath = path.replace("\\en_US\\", "\\ko_KR\\");

This is my actual code.  I'm using a Pattern and referencing the pattern .toString method to get the string value (which is "\\en+US\\" and "\\ko_KR\\")

String newString = parts[0].replace(flexPattern.toString(), localePattern.toString());

But my newString contains "\\en_US\\" instead of "\\ko_KR\\", so it's not working.
0
 

Author Comment

by:pdlarue
ID: 37820535
Actual code:

    private String bulidFileName(String base, String locale) {
        String newFileName = "";
        Pattern flexPattern = Pattern.compile("\\\\en_US\\\\");
        Pattern localePattern = Pattern.compile(findLocale(locale));

        Matcher reportsMatcher = reportsPattern.matcher(base);
        Matcher flexMatcher = flexPattern.matcher(base);
       
        if (flexMatcher.find()) {
            System.out.println(base);
            String regexp = "\\.";
            String [] parts = base.split(regexp);
            System.out.println(parts[0]);
            System.out.println(parts[1]);
            String newString = parts[0].replace(flexPattern.toString(), localePattern.toString());
            System.out.println(newString);
            newFileName = parts[0] + "_" + locale + "." + parts[1];  
            System.out.println(newFileName);
        } else {
            System.out.println(base);
            String regexp = "\\.";
            String [] parts = base.split(regexp);
            System.out.println(parts[0]);
            System.out.println(parts[1]);        
            newFileName = parts[0] + "_" + locale + "." + parts[1];  
            System.out.println(newFileName);
        }
        return newFileName;
    }

          private String findLocale (String locale) {
        String locationLocale = "";
        Iterator it = locales.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pairs = (Map.Entry)it.next();
            System.out.println(pairs.getKey() + " = " + pairs.getValue());
            locationLocale = pairs.getKey().toString();
            if (locationLocale.startsWith(locale) && locationLocale.length() > 2) {
                locationLocale = "\\\\" + locationLocale + "\\\\";
            }
        }
        return locationLocale;
    }
0
 

Author Comment

by:pdlarue
ID: 37820539
The pattern requires the 4 "\\\\" but the string value contains only 2 "\\".
0
 

Author Comment

by:pdlarue
ID: 37820544
In the if (flexMatcher.find()) condition, ignore this line:

            newFileName = parts[0] + "_" + locale + "." + parts[1];  

The issue is in this line:

            String newString = parts[0].replace(flexPattern.toString(), localePattern.toString());

Do I have to handle the pattern.toString() differently than a litteral string?
0
 

Author Comment

by:pdlarue
ID: 37820558
The pattern.toString() is different.  I mean it looks the same but doesn't work.  I replaced that line with this one:

            String testString = parts[0].replace("\\en_US\\", "\\ko_KR\\");
 
and it worked.
0
 

Author Closing Comment

by:pdlarue
ID: 37820583
Thank you.  That helped me to figure out why it wasn't working.  :-)
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

I'd like to talk about something that is near and dear to my heart: build systems. Without them, building software is all about compiling locally, with software versions everywhere. It can be a mess. Today we are going to discuss building a small di…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

813 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now