Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Java Replace string

Posted on 2012-04-07
9
Medium Priority
?
460 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 2000 total points
ID: 37820398
sorry this:
String newPath = path.replace("\\en_US\\", "\\ko_KR\\");
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

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

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…
How many times a day do you open, acknowledge, or close an IT incident? What’s your process? Do you have a process depending on the incident, systems involved, and other factors? New Relic Alerts gives you options for how you interact with notifica…
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.
Suggested Courses

604 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