首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java :有没有办法知道字符串中原始数据的类型?

Java :有没有办法知道字符串中原始数据的类型?
EN

Stack Overflow用户
提问于 2016-02-26 12:19:56
回答 3查看 240关注 0票数 2

我有一个相同基本类型的字符串数组(不知道是什么)。我需要把它转换成相应的类型。如果我传递一个字符串并获得该字符串中数据的基元类型(如果有的话),是否有任何方法。假设字符串应该具有基本类型,或者将其视为字符串。

为e-g

代码语言:javascript
复制
"5.2" - double
"5" - int (most related)
"s" - char 

所以,如果是double,我可以使用Double.parseDouble进行解析。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-02-26 12:22:02

不那是不可能的。例如,这个输入:

代码语言:javascript
复制
5
  • 这可能是一个int
  • 但也可以是字节shortlong
  • 它可以是一个浮点或一个
  • 它甚至可以是一个char

我们所知道的唯一的事情是,它可能不是一个布尔

当然,您也可以尝试像这样解析您的字符串:

代码语言:javascript
复制
public static void main(String[] args) {
    checkType("5.2");
    checkType("5");
    checkType("s");
}

protected static void checkType(String input) {
    System.out.println(input);
    try {
        byte result = Byte.parseByte(input);
        System.out.println("could be interpreted as byte "+result);
    } catch (NumberFormatException e) {
        System.out.println("not an byte");
    }
    try {
        short result = Short.parseShort(input);
        System.out.println("could be interpreted as short "+result);
    } catch (NumberFormatException e) {
        System.out.println("not an short");
    }
    try {
        int result = Integer.parseInt(input);
        System.out.println("could be interpreted as int "+result);
    } catch (NumberFormatException e) {
        System.out.println("not an int");
    }
    try {
        long result = Long.parseLong(input);
        System.out.println("could be interpreted as long "+result);
    } catch (NumberFormatException e) {
        System.out.println("not an long");
    }
    try {
        float result = Float.parseFloat(input);
        System.out.println("could be interpreted as float "+result);
    } catch (NumberFormatException e) {
        System.out.println("not an float");
    }
    try {
        double result = Double.parseDouble(input);
        System.out.println("could be interpreted as double "+result);
    } catch (NumberFormatException e) {
        System.out.println("not an double");
    }
    try {
        boolean result = Boolean.parseBoolean(input);
        System.out.println("could be interpreted as boolean "+result);
    } catch (NumberFormatException e) {
        System.out.println("not a boolean");
    }
    if (input.length() == 1) {
        System.out.println("could be interpreted as character "+input);
    } else {
        System.out.println("not a character");
    }
    System.out.println();
}

对于给出的例子,它的产出如下:

代码语言:javascript
复制
5.2
not an byte
not an short
not an int
not an long
could be interpreted as float 5.2
could be interpreted as double 5.2
could be interpreted as boolean false
not a character

5
could be interpreted as byte 5
could be interpreted as short 5
could be interpreted as int 5
could be interpreted as long 5
could be interpreted as float 5.0
could be interpreted as double 5.0
could be interpreted as boolean false
could be interpreted as character 5

s
not an byte
not an short
not an int
not an long
not an float
not an double
could be interpreted as boolean false
could be interpreted as character s
票数 5
EN

Stack Overflow用户

发布于 2016-02-26 12:39:42

您不能自动猜测String的内容,但是如果您有一组规则,则可以创建一个解析内容的方法。

编写一个Identifier类,该类可以标识String的类型,然后调用关联的解析器来检索原语值。

下面是一组与您的问题相匹配的正则表达式:

  • double = regex:^[0-9]+\.[0-9]+$,解析器:Double.parseDouble(String)
  • int = regex:^[0-9]+$,解析器:Integer.parseInt(String)
  • char = regex:^[a-zA-Z]$,解析器:value.charAt(0)

附带注意:这组正则表达式不处理负值。另外,在使用NumberFormatExceptionparseInt时,不要忘记检查是否存在parseDouble

票数 2
EN

Stack Overflow用户

发布于 2016-02-26 12:47:28

Java是字符的抽象,只看到字符。

字符串本身不知道它里面有什么样的表示,只是它是一个同构的(排他的)字符集合。

如果你被"+“操作符误导了。这个操作符浓缩了您要连接到对象的内容,并且当对象被连接时,它被转换成字符串,因此数据确实丢失了。

您需要自己分析字符串,以确定它是否包含对您有意义的信息,并将其提取出来。

创建能够存储此类信息的自己的字符串是可能的,但它会使构造臃肿,因为您很少需要使用这些特性,而且它们也可以在外部使用。

发布你想要完成的事情,而不是抽象的可能性声明,会更有帮助。答案是肯定的,您可以这样做,但是字符串不会帮助您这样做,您需要自己解析字符串。

请参阅"http://asciivalue.com/“,以查看您输入的任何字符串基本上都隐藏了这些数据(通常也包括终止值0)。如果它必须存储您在问题中询问的数据,那么存储起来就太昂贵了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35651567

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档