how do I detect '\n' in a string??  | |
September 25th, 2002, 11:01 PM
|
#1 (permalink)
| | Senior Member
Join Date: Oct 2001
Posts: 881
| how do I detect '\n' in a string??
I'm using java, and I have a string with '\n' in it...
do I have to go to the byte level to detect this? or is there some trickier way around it?
i want to be able to read the string as if I were reading it line by line from a file
-Z |
| |
September 25th, 2002, 11:36 PM
|
#2 (permalink)
| | Member
Join Date: Sep 2002
Posts: 364
|
I would use a loop and loop through each character until I found the new line. I've use this method in other languages to fish things out before. Like removing single quotes from strings in VB.
You ought to write a method that you can reuse and feed strings to. |
| |
September 25th, 2002, 11:49 PM
|
#3 (permalink)
| | Senior Member
Join Date: Oct 2001
Posts: 881
|
hmm... well, will a '\n' even show up if I'm reading by char? I thought that it was embeded even lower.. although char is primative... i"ll check
-Z |
| |
September 26th, 2002, 12:09 AM
|
#4 (permalink)
| | Member
Join Date: Jul 2002 Location: Illinois
Posts: 91
|
I would think you can come up with it using char (it's low level enough). I know I've parsed it using C++ and Assembler, but I don't know about Java. I'd try what was suggested ... are you trying to read it from input or from a file? I didn't really understand what you meant in your question. If you put it in the string or the string came from reading in a line of a file (ended with a \n), I think you should be able to pick it up using the character loop, but if it's user input and you want to pick that up, you may have to use Javas equivelent to getch(); or get.char(char_var); commands and let them input one character at a time, and check first to see if it's \n before appending it to your string
__________________
You may only be one person in the world, but you can be the whole world to one person.
|
| |
September 26th, 2002, 12:28 AM
|
#5 (permalink)
| | Member
Join Date: Sep 2002
Posts: 364
|
Yeah '\n ' is a char. Test it if you'd like.
If you say char c = '\n'; I bet it will work.
You could do this many ways. You could use the getChars() method and have all the characters be put into an array, then loop through the array, looking for the \n.
Keeping making your new string from the char array until you've reached an \n, then start making another new string.
Last edited by Creosote : September 26th, 2002 at 02:42 AM.
|
| |
September 26th, 2002, 02:03 AM
|
#6 (permalink)
| | Senior Member
Join Date: Oct 2001
Posts: 881
|
worked great.. thanks for the help with that!
-Z |
| |
September 26th, 2002, 07:29 PM
|
#7 (permalink)
| | Ultimate Member
Join Date: Oct 2001
Posts: 1,542
|
Do be aware that different systems use different line terminators... in Unix it's \n, in Windows \r\n, in Mac \r...
If your task is to separate a string at line terminators, your best bet would be to use java.util.regex (the regular expressions package) and search on "$" (the end-of-line boundary matcher) or on "[^.]" ("not anything-but-a-line-terminator")
Another thing you could do is something like this: Code: BufferedReader br = new BufferedReader(new StringReader(inputString));
StringBuffer sB = new StringBuffer("");
String s;
while (true) {
try {s = in.readLine();}
catch (IOException e) {s = null;}
if (s==null) break;
sB.append(s).append('\n');
}
return new String(sB); This certainly fulfils the "as if I were reading it line by line from a file" requirement!  |
| |
September 26th, 2002, 08:39 PM
|
#8 (permalink)
| | Senior Member
Join Date: Oct 2001
Posts: 881
|
hmm... well, the thing is, i'm writing this proggie for Eudora which (as far as I know) is only windows compliant... in the end,that worked fine becaue I could just search for the \n in order to find the end of the line, and then tokenize that string w/ stringTokenizer which doesn't see lowerlevel stuff like \n!
good idea though stranger, thanks!
-Z |
| | | Thread Tools | Search this Thread | | | | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |