| Author |
Convert "\n" to char '\n'
|
pioncz Jonnish
Greenhorn
Joined: Jan 12, 2012
Posts: 3
|
|
Hello,
How can i convert string "\n" to char '\n' with not if or switch?
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16480
|
|
|
The easiest way to get the first character of a String as a char is "theString.charAt(0)". It makes no difference what that character is, either. Was that what you had in mind?
|
 |
pioncz Jonnish
Greenhorn
Joined: Jan 12, 2012
Posts: 3
|
|
|
No, first char is '\\' and second is 'n'. I want to make one char from both of them - new line char.
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9939
|
|
I would start by looking at the String api, and see if there is a method that may help you...
[edit] posted above before seeing replies[\edit]
So your string has two characters - a backslash character and a 'n' character?
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5793
|
|
Your question doesn't make much sense without more context. Will you always have a one-character String? If not what are your rules for finding a "\n" and using that to create a '\n'?
Please provide some details about what you're trying to accomplish and what you mean by "convert "\n" to '\n'" to make your question clearer.
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5793
|
|
pioncz derpina wrote:No, first char is '\\' and second is 'n'. I want to make one char from both of them - new line char.
So, you're saying if you encounter a '\' character followed by a 'n' character, you want to convert that to a '\n' character? If it's exactly that, then it's easy. However, if the '\' character can be used to escape itself for a literal '\', then it gets more difficult. That is, if the sequence 'a' '\' \n'\ 'b' needs to become 'a' '\n' 'b', but the sequence 'a' '\' '\' 'n' 'b' needs to become 'a' '\' 'n' 'b', then it's a little more work.
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16480
|
|
|
So you don't have the string "\n". You have the string "\\n" which has two characters: a backslash character and a letter-n character. To replace this substring by the "\n" string you can use
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
|
|
Welcome to the Ranch
|
 |
pioncz Jonnish
Greenhorn
Joined: Jan 12, 2012
Posts: 3
|
|
So here are more informaction.
I have file with some line (every file can have different lines).
This file looks like <string_to_search>|<string_to_past>
But there could be not only normal string, special chars could be there too (like new line so). This file can't look like:
|<string_to_past>
So i decided to write special chars like they are normal: \n,\r or more. I will know when is special char because, first char is '\n'. So now i have problem, when first char is '\' how can i search for things in file like "\n". I have 2 ways: 1) make table with special chars, so when second character is 'n' i will search in file for special_chars[1] (where it's exacly '\n'), but it's uglier way. 2) I will convert from this string with special char to char ("\n" to '\n' with some algorithm or function).
And also welcome : )
|
 |
 |
I agree. Here's the link: jrebel
|
|
subject: Convert "\n" to char '\n'
|
|
|