Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convert "\n" to char '\n'

 
pioncz Jonnish
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
How can i convert string "\n" to char '\n' with not if or switch?
 
Paul Clapham
Sheriff
Posts: 21149
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12147
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21149
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49447
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
pioncz Jonnish
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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")
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic