Solved

Java Replace string

Posted on 2012-04-07
9
450 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
 

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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

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…
Cloud-based technologies and services will continue to grow in popularity in 2017 thanks to the simple, scalable and cost-effective solutions they deliver. Here are three areas where cloud adoption is poised to really take off.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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.

911 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

21 Experts available now in Live!

Get 1:1 Help Now