4

I am working for a java desktop application which parse logs and upload to server. 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 -

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

because my parser code accepts separator in char.

The issue is when user provides "\t" then how can I provide separator in char to my parser. 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.

Ashish Pancholi
  • 4,569
  • 13
  • 50
  • 88

5 Answers5

4

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

if( sTerminatedBy.equals("\\t"))
    seperator = '\t';
Bala R
  • 107,317
  • 23
  • 199
  • 210
1
if ("\\t".equals(sTerminatedBy)) {
  separator = '\t';
} else if (null == sTerminatedBy || "".equals(sTerminatedBy)) {
  separator = ' ';
} else {
  separator = sTerminatedBy.charAt(0);
}
Christoffer Hammarström
  • 27,242
  • 4
  • 49
  • 58
  • It'd be nice to do this automatically, for any backslash or other valid Java string, so that it would handle "\\t", "\\n", "\\u1234", etc – Mark Bennett Dec 05 '14 at 19:08
  • Ah, here's the generic version for various escape sequences, http://stackoverflow.com/questions/3537706/howto-unescape-a-java-string-literal-in-java – Mark Bennett Dec 05 '14 at 19:25
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. The UDF has a limitation to accept only string arguments. What but my parser later is required char to define the separator. 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 e.g. for TAB ('\t') I used the number 9. Than I converted my string arg ("9") ro int and I converted the int to char.

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

Output for "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.

kecso
  • 2,387
  • 2
  • 18
  • 29
0

This is a true expression:

"\t".charAt(0) == '\t'
Jeremy
  • 22,188
  • 4
  • 68
  • 81
0

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

Tom
  • 2,734
  • 2
  • 22
  • 39