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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Splitting by enter " Watch "Splitting by enter " New topic
Author

Splitting by enter

Carlos Valderrama
Greenhorn

Joined: Feb 20, 2013
Posts: 14
Hi guys! I hope you're all ok!

In my last post and thanks to your help I managed to split the text from a text area into lines and add a "\n" and the end of each line. After that I succesfully updated the field in the database.

So technically the text in the field has been updated to: line1"\n"line2"\n"line3"\n"line4 ( eventhough I don't get to see the "\n" character, I know that it has worked because when I copy-paste the field

directly from the database into a text editor it respects the "\n".


Well, now when I read the string from database and try to apply any of the following methods to the String:

myString.split("\n") , myString.split("\\n") , myString.split("\n\r"), myString.split("[\r\n] "), myString.split("[\r\n] "), myString.split("\\r?\\n"), myString.split(System.getProperty("line.separator"))

none of them has worked

So again I would really appreciate any comment or suggestion to try!

Thanks in advance !!











Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

This is usually an escaping problem. The characters you need to escape have to go through two levels of escape: first one for the Java String, and a second for the RegeEx. So try string.split("\\\\n") but maybe string.split("[\\n]") would work as well. How exactly do you add the characters to the string? And how do you know it is or is not working?


Steve
Carlos Valderrama
Greenhorn

Joined: Feb 20, 2013
Posts: 14
Hi Steve thank you for replying me!

Look, a field value from the database looks like this:

Before

AV CONSTITUCION RES LOS MANGOS TORRE F PISO 14 TORRE 145F


After:


Adding at the end of each splitted line with line+"\n";
AV CONSTITUCION RES LOS MANGOS

TORRE F PISO 14 TORRE 145F

It actually is stored like this in the database.

And i know it works because I copy-paste the field in a text editor and the "\n" are respected.

My question is

why can't i get to split it into an array of splitted parts?


I'm using

String[] ArrayParts = dirOrd.split("[\\n]"); ( with any of the other it doesn't works either)

I'm guessing it could have something to do with the library to retrieve values from the database? sqljdbc4.jar?



Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

Steve Luke wrote:How exactly do you add the characters to the string? And how do you know it is or is not working?


These questions still remain unanswered.

Add to that, what does the string look like when you get it from the DB using your library before you process it?

I believe the "\n" is there, but how - specifically - showing code - do you add it? And how do you know it is not splitting? You show that you do the split. And you say that it does not work. But what does it do, how do you know what it is doing, and how do you prove it is or is not working the way you expect it?
Carlos Valderrama
Greenhorn

Joined: Feb 20, 2013
Posts: 14
Steve,

Some replacements where applyed to the string after I retrieved it from database.. including one [\\n\\t]

I'm really ashamed with you, because I took part of your time when i simply needed to debug it more carefully.

Thanks for you support.

I apologize



Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

No reason to feel ashamed, we all need help, and the forums are a great place to get it. I just hope something I said was helpful.
 
Consider Paul's rocket mass heater.
 
subject: Splitting by enter
 
Similar Threads
insert header and footer with POI HWPF?
Update Method, Validation, RuntimeException
Java IO Problem
date fomatting question from long
matching number from set of numbers.