How to convert string = "\ t" to char

I am working on a Java desktop application that parses logs and uploads them to a server. We ask the user to provide a separator, by which we parse the CSV file, and read the provided separator from the text field in the line and do char by -

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

because my parser code accepts a delimiter in char.

The problem is when the user provides "\ t", then how can I provide a delimiter in char for my analyzer. The user can request parsing using any separator so that any body suggests what I can do for general code and can provide a separator in char.

+3
source share
5 answers
if ("\\t".equals(sTerminatedBy)) {
  separator = '\t';
} else if (null == sTerminatedBy || "".equals(sTerminatedBy)) {
  separator = ' ';
} else {
  separator = sTerminatedBy.charAt(0);
}
+1

?

char tab = '\t';

, "\\ t", if

if( sTerminatedBy.equals("\\t"))
    seperator = '\t';
+4

:

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

/- ( ). java UDF (User Defined Function) Pig. UDF , . , char. , char char. --. . , , TAB ('\ t') 9. string arg ( "9" ) ro int, int char.

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

"9":

[   ]

Not the most pleasant solution, but you do not need to encode all possible control characters for your code. But keep in mind that the caller is using the correct decompression representation of ctrl char.

0
source

Consider:

String something = "This is \ yew \ TTAB \ tseparated";

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);
}

Greetings

-1
source

All Articles