简体   繁体   English

如何将字符串 = "\\t" 转换为字符

[英]How to convert string = "\t" to char

I am working for a java desktop application which parse logs and upload to server.我正在为一个 java 桌面应用程序工作,它解析日志并上传到服务器。 We ask user to provide separator by which we parse CSV file and we read provided separator from text field in string and make a char by -我们要求用户提供用于解析 CSV 文件的分隔符,我们从字符串中的文本字段中读取提供的分隔符并通过以下方式生成字符 -

separator = (sTerminatedBy != null && !sTerminatedBy.equalsIgnoreCase("")) ? sTerminatedBy.charAt(0) : ' ';

because my parser code accepts separator in char.因为我的解析器代码接受 char 中的分隔符。

The issue is when user provides "\\t" then how can I provide separator in char to my parser.问题是当用户提供 "\\t" 时,我如何向解析器提供 char 中的分隔符。 User can request to parse by any separator so can any body suggest what can I do to generic my code and can provide separator in char.用户可以请求通过任何分隔符进行解析,因此任何机构都可以建议我可以做什么来通用我的代码,并可以在 char 中提供分隔符。

Can't you use this?你不能用这个吗?

char tab = '\t';

If it's user input, then the actual string would be "\\\\t" so you'll have to resort to using if如果是用户输入,则实际字符串将是“\\\\t”,因此您将不得不求助于使用if

if( sTerminatedBy.equals("\\t"))
    seperator = '\t';
if ("\\t".equals(sTerminatedBy)) {
  separator = '\t';
} else if (null == sTerminatedBy || "".equals(sTerminatedBy)) {
  separator = ' ';
} else {
  separator = sTerminatedBy.charAt(0);
}

Here's a late answer/work-a-round for the same (or similar question).这是同一(或类似问题)的迟到答案/解决方案。 I'm faced a similar issue in case of a java UDF (User Defined Function) for Pig.在 Pig 的 Java UDF(用户定义函数)的情况下,我遇到了类似的问题。 The UDF has a limitation to accept only string arguments. UDF 有一个限制,即只能接受字符串参数。 What but my parser later is required char to define the separator.但是我的解析器稍后需要 char 来定义分隔符。 I didn't want to hardcode the separator so I faced the string control char to char conversion problem.我不想对分隔符进行硬编码,所以我面临着字符串控制字符到字符的转换问题。 So here's my work-a-round.所以这是我的工作。 I as argument I used the decimal representation of the control character.我作为参数我使用了控制字符的十进制表示。 So eg for TAB ('\\t') I used the number 9. Than I converted my string arg ("9") ro int and I converted the int to char.例如,对于 TAB ('\\t'),我使用了数字 9。然后我将字符串 arg ("9") 转换为 ro int,然后将 int 转换为 char。

int tab = Integer.parseInt(args[1]);
char ch = (char) tab;
System.out.println("[" + ch + "]"); 

Output for "9": “9”的输出:

[   ]

Not the nicest solution, but you don't have to code all the possible control characters to your code.不是最好的解决方案,但您不必将所有可能的控制字符编码到您的代码中。 But have to aware that the caller using the right decumal representation of the ctrl char.但是必须知道调用者使用了正确的 ctrl 字符的十进制表示。

这是一个真实的表达:

"\t".charAt(0) == '\t'

You could also use an dictionary which will contains any delimiters you want:您还可以使用包含您想要的任何分隔符的字典:

delimiters = {
    "\\t" : '\t',
    "\\r" : '\r'
}

etc.等等。

And finally check if \\\\t in delimiters then get value最后检查\\\\t在分隔符中,然后获取值

Consider:考虑:

String something ="this\\tis\\ttab\\tseparated"; String something ="this\\tis\\ttab\\tseparated";

The recommended approach:推荐的方法:

// Can be also used with files and streams
Scanner sc = new Scanner(something);
sc.useDelimiter("\t");

while (sc.hasNext()) {
   System.out.println(sc.next());
}
sc.close();

And for small inputs:对于小输入:

String[] separated = something.split("\t");
for (String string : separated) {
   System.out.println(string);
}

Cheers,干杯,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM