开发者

Convert timezone name to city name (remove continent part)

I was wondering if anyone could help me figure out to to remove part of a string. I am using the java.util.TimeZone to retrieve time zone "ids" but I want to delete the continent/ prefix, basically stripping out the city name. I also have to replace all underscores with spaces which I have done in code already. I just can not seem to find a way to get rid of the backslash and everything before it. Here is my code. Thanks

import java.util.*;
import java.util.TimeZone;
import java.util.Date;
import jav开发者_C百科a.text.DateFormat;

public class Maps {

public static void main(String[] args) {

  String[] Zone = TimeZone.getAvailableIDs();

  int i = 0;

   for (i =0 ; i< Zone.length; i++) {
    String zone1 = Zone[i].replaceAll("_"," "); 

    System.out.println(zone1);
   }   
 }        
}


So you want to remove everything before the first slash, and then replace any underscores in the remainder with spaces? That's fairly easy using indexOf, substring and replace (which you're already aware of).

You certainly could do this with regular expressions as suggested by Mr_Spock, but personally for just a couple of operations I'd stick with the normal string methods:

int slash = text.indexOf('/');
text = text.substring(slash + 1).replace("_", " ");

So for a complete example:

import java.util.TimeZone;

public class Test {

    public static void main(String[] args) {        
        String[] ids = TimeZone.getAvailableIDs();

        for (String id : ids) {
            int slash = id.indexOf('/');
            String stripped = id.substring(slash + 1).replace("_", " ");
            System.out.println(stripped);
        }
    }   
}

Note that this still leaves you with some interesting names such as "North Dakota/Beulah" as the original ID had two slashes in.

Just for completeness, another way of handling the slash is to split the string on slash and then take the second part - but as the above example shows, you then probably want to make sure that it splits into at most two parts, keeping "North Dakota/Beulah" as a single token. You'd also need to be careful in case there were any IDs without any slashes (the above code works fine as indexOf will return -1, so the substring will become a no-op).


I would try to approach this in an unorthodox fashion for the average Java coder (I assume). I'd try to utilize the power of regular expressions.

Try exploring the world of java.util.regex

It doesn't hurt to learn about RegEx, as it's used in string manipulation solutions across the board.

I hope that helps.


Check this out :

              public static void main(String[] args) {

        String[] Zone = TimeZone.getAvailableIDs();

         int i = 0;

       for (i =0 ; i< Zone.length; i++) {
        String zone1 = Zone[i].replaceAll("_"," ");

        if(zone1.indexOf('/') != -1)
          zone1 = zone1.substring(zone1.indexOf('/')+1);

        System.out.println(zone1);
       }   
}


You can use just split the zone :-

String zone1 = Zone[i];
zone1 = (zone1.split("/").length > 1) ? zone1.split("/")[1] : zone1.split("/")[0];
zone1 = zone1.replaceAll("_"," "); 


 if(zone1.indexOf("/")!=-1) {
        zone1=zone1.substring(zone1.indexOf("/")+1,zone1.length());
        System.out.println(zone1);
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜