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,459
  • 10
  • 46
  • 84

6 Answers6

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
  • 104,615
  • 23
  • 192
  • 207
1
if ("\\t".equals(sTerminatedBy)) {
  separator = '\t';
} else if (null == sTerminatedBy || "".equals(sTerminatedBy)) {
  separator = ' ';
} else {
  separator = sTerminatedBy.charAt(0);
}
Christoffer Hammarström
  • 26,003
  • 4
  • 45
  • 56
  • 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,297
  • 2
  • 17
  • 27
0

This is a true expression:

"\t".charAt(0) == '\t'
Jeremy
  • 21,745
  • 4
  • 64
  • 79
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,344
  • 1
  • 20
  • 34
-1

Consider:

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,

Anthony Accioly
  • 20,718
  • 8
  • 67
  • 109
  • -1: You completely misunderstood the question. He wants the user to provide the separator, and `"\\t"` should be interpreted as `'\t'`. – Christoffer Hammarström May 05 '11 at 15:22
  • @Christoffer, if you say so... Your answer got accepted by the OP after all. Still, your solution pointed @Ashish to a case by case scenario (he will need to deal with `\\n`, etc). My solution pointed him to the standard generic APIS that do what he wants in the right way, same as @JustinKSU. – Anthony Accioly May 05 '11 at 16:28
  • The op asked how to convert `"\\t"` to `'\t'`, and you told him how to split a string. Complete non sequitur. – Christoffer Hammarström May 05 '11 at 16:30
  • Read the second part of his question and see what he is trying to accomplish with his CSV 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.". Well, no use arguing, if you think I gave bad advice and made a dangerously wrong misinterpretation, then fine, I can live with it. – Anthony Accioly May 05 '11 at 16:39
  • "User can request to parse by any separator" means "User will input a separator as a string". "what can I do to generic my code and can provide separator in char" means "How do i convert this string to a single character?". The question is about how to interpret the separator string input by the user and your answer addressed none of that. – Christoffer Hammarström May 05 '11 at 16:42
  • My answer pointed to a solution that avoids the conversion step all together and uses the right tools to handle generic delimiters avoiding case by case scenarios (like yours). I surely didn't grant his separator in char wish, but pointed out how he can generalize his code to achieve what we wants. – Anthony Accioly May 05 '11 at 16:54
  • You didn't point out how to achieve what he wants, but answered something else entirely. Which makes it not an answer, but completely off topic. You even hard coded the separator when the question was about how to get the separator from the user. He had one requirement in addition to what he already had working: Allow the user to type `\t` to mean tab, and my answer fulfilled that requirement while your answer was completely off topic. Even if you are imagining other possible requirements for other separators, your answer didn't even fulfill those. – Christoffer Hammarström May 05 '11 at 21:25