当前位置: 首页 > 知识库问答 >
问题:

从字符串中删除HashMap键

拓拔麒
2023-03-14

我在数据库表列中有手机号码,格式为country_code,后面是mobile_number,所以手机号码格式是这样的,

+91123456789 // country code of India is +91 followed by mobile number
+97188888888 // Country code of UAE +971

我有一个HashMap包含像这样的5个国家的国家代码,

map.put("+91","India")
map.put("+94","Sri Lanka")
map.put("+881","Bangladesh")
map.put("+971","UAE")
map.put("+977","Nepal")

我的豆子结构是这样的

class UserDetails {

  // other fields
   String countryCode;
   String mobileNumber;
}

现在,我的任务是从数据库表列中提取手机号码,并将其分成两部分,然后设置countryCodemobileNumber,但国家代码长度(在地图键中)在3到4之间变化。这个检查可以通过使用subString()equals()来完成,但我认为这不是正确的方法,那么解决这个问题的优雅(可能是签入映射键)方法是什么呢?

共有3个答案

何向荣
2023-03-14

您可以使用两个不同长度的国家代码地图,然后首先搜索3个字母的匹配,然后搜索4个字母的匹配。

    HashMap<String, String > threeLetterCodes = new HashMap<String, String>();
    threeLetterCodes.put("+91","India");
    threeLetterCodes.put("+94","Sri Lanka");


    HashMap<String, String > fourLetterCodes = new HashMap<String, String>();        
    fourLetterCodes.put("+881","Bangladesh");
    fourLetterCodes.put("+971","UAE");
    fourLetterCodes.put("+977","Nepal");

    String test = "+97188888888";

    String prefix = test.substring(0, 3);
    String country = threeLetterCodes.get(prefix);
    if (country == null) {
        prefix = test.substring(0, 4);
        country = fourLetterCodes.get(prefix);
    }

    System.out.println(country);

输出:

UAE
西门飞星
2023-03-14

我觉得一张地图更好。一个例子;

public static void main(String args[]) throws Exception {
    Map<String, String> map = new HashMap<>();
    put(map, "+91", "India");
    put(map, "+94", "Sri Lanka");
    put(map, "+881", "Bangladesh");
    put(map, "+971", "UAE");
    put(map, "+977", "Nepal");
    map = Collections.unmodifiableMap(map);

    String mobileNumber = "+91123456789";
    System.out.println(countryCode(map.keySet(), mobileNumber));
}

private static void put(Map<String, String> map, String key, String value) {
    if (countryCode(map.keySet(), key) != null) {
        throw new IllegalArgumentException("...");
    }
    map.put(key, value);
}

public static String countryCode(Set<String> countryCodes, String number) {
    if (number == null || number.length() < 3) {
        throw new IllegalArgumentException("...");
    }
    String code = number.substring(0, 3);
    if (!countryCodes.contains(code)) {
        if (number.length() > 3) {
            code = number.substring(0, 4);
            if (!countryCodes.contains(code)) {
                code = null;
            }
        } else {
            code = null;
        }
    }
    return code;
}
裴泰平
2023-03-14

虽然有一个库似乎已经做到了这一点,但我想我会选择一个简单的自编解决方案:

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class CountryExtractor {
    private static final Map<String, String> COUNTRY_MAPPING = new HashMap<>();

    static {
        COUNTRY_MAPPING.put("+91", "India");
        COUNTRY_MAPPING.put("+94", "Sri Lanka");
        COUNTRY_MAPPING.put("+881", "Bangladesh");
        COUNTRY_MAPPING.put("+971", "UAE");
        COUNTRY_MAPPING.put("+977", "Nepal");
    }

    public static void main(String[] args) {
        String[] inputs = new String[] { "+91123456789", "+97188888888" };

        for (String input : inputs) {
            System.out.println(Arrays.toString(parseNumber(input)));
        }
    }

    private static String[] parseNumber(String number) {
        for (String countryCode : COUNTRY_MAPPING.keySet()) {
            if (number.startsWith(countryCode)) {
                return new String[] { countryCode, number.replace(countryCode, "") };
            }
        }
        return new String[0];
    }
}

输出:

[+91, 123456789]
[+971, 88888888]

请注意,当移动前缀是另一个前缀的子串时,这可能无法正常工作,但根据维基百科,国家调用代码是前缀代码,因此保证“系统中没有整个码字是任何其他前缀(初始段)系统中的码字”。

 类似资料:
  • 问题内容: 我使用的框架会不时返回格式错误的字符串,其中包含“空”字符。 例如,“ foobar”表示为:[,f,o,o,b,a,r] 第一个字符不是空格(’‘),因此System.out.printlin()将返回“ foobar”而不是“ foobar”。但是,String的长度是7,而不是6。显然,这使大多数String方法(等于,拆分,子字符串等)变得无用。有没有办法从字符串中删除空字符?

  • 问题内容: 我正在考虑使用删除字符串中的某些字符。目前尚不清楚将要删除哪些字符(即我想删除哪些字符),但是我会假定任何字符都是有效的(例如和之类的东西,等等)。 我遇到了http://www.java-tips.org/java-se-tips/java.lang/strip-certain-characters- from-a-string.html, 但是肯定有比遍历每个字符更好的方法… 有什

  • 问题内容: 我在从字符串中删除非utf8字符时出现问题,这些字符无法正确显示。像这样的字符0x97 0x61 0x6C 0x6F(十六进制表示) 删除它们的最佳方法是什么?正则表达式还是其他? 问题答案: 使用正则表达式方法: 它搜索UTF-8序列,并将其捕获到组1中。它还与无法标识为UTF-8序列的一部分的单个字节匹配,但不捕获这些字节。替换是捕获到组1中的任何内容。这将有效删除所有无效字节。

  • 问题内容: 从网站提取数据时出现奇怪的字符: 如何删除不是非扩展ASCII字符的内容? 问题答案: 正则表达式替换将是最佳选择。使用作为一个例子的字符串,并使用匹配它,这是一个POSIX字符类: 什么是寻找所有可打印字符。相反,查找所有不可打印的字符。不属于当前字符集的所有字符都将被删除。 注意: 使用此方法之前,必须确保当前字符集为ASCII。POSIX字符类同时支持ASCII和Unicode,

  • 假设我的字符串是10个字符长。 如何删除最后一个字符? 如果我的字符串是(我不想替换字符,因为我的字符串可能包含多个字符),我只想删除最后一个字符。不管它是什么或者它发生了多少次,我都需要从我的字符串中移除最后一个字符。

  • 我想转动这根绳子: 到这个里面 用似乎没有明显的方法来做到这一点? 更准确地说,我想将反斜杠的转义改为转义字符。