Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Convert "\n" to char '\n'

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
How can i convert string "\n" to char '\n' with not if or switch?
 
Marshal
Posts: 26495
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 12975
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 26495
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 72492
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
pioncz Jonnish
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 : )
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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")
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Try this :  "Test \nString".replaceAll("\n",""+(char)10 )
 
Once upon a time there were three bears. And they were visted by a golden haired tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic