Solved

Java Replace string

Posted on 2012-04-07
9
455 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
[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
  • 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
Changing a few Outlook Options can help keep you organized!
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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.

732 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