Solved

Java Replace string

Posted on 2012-04-07
9
447 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
In this article, I will show you HOW TO: Install VMware Tools for Windows on a VMware Windows virtual machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, using the VMware Host Client. The virtual machine has Windows Server 2016 instal…
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.

746 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

12 Experts available now in Live!

Get 1:1 Help Now