wood burning stoves*
The moose likes Java in General and the fly likes Convert Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Convert "\n" to char Watch "Convert "\n" to char New topic
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: 18570
    
    8

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: 11352
    
  16

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?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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: 6109
    
    6

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: 18570
    
    8

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: 39072
    
  23
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 : )
Trung Dang
Greenhorn

Joined: Nov 10, 2013
Posts: 1
pioncz Jonnish wrote:Hello,
How can i convert string "\n" to char '\n' with not if or switch?


"Test \n String".replaceAll("\n",""+(char)92+(char)92+"n")
 
Consider Paul's rocket mass heater.
 
subject: Convert "\n" to char '\n'