home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

how do I detect '\n' in a string??

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 2270
Discussions: 200,970, Posts: 2,379,741, Members: 246,335
Old September 25th, 2002, 11:01 PM   Digg it!   #1 (permalink)
Senior Member
 
Join Date: Oct 2001
Posts: 881
Send a message via AIM to zskillz
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
zskillz is offline   Reply With Quote
Old 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.
Creosote is offline   Reply With Quote
Old September 25th, 2002, 11:49 PM     #3 (permalink)
Senior Member
 
Join Date: Oct 2001
Posts: 881
Send a message via AIM to zskillz
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
zskillz is offline   Reply With Quote
Old September 26th, 2002, 12:09 AM     #4 (permalink)
Member
 
Join Date: Jul 2002
Location: Illinois
Posts: 91
Send a message via AIM to kickzum
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.
kickzum is offline   Reply With Quote
Old 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.
Creosote is offline   Reply With Quote
Old September 26th, 2002, 02:03 AM     #6 (permalink)
Senior Member
 
Join Date: Oct 2001
Posts: 881
Send a message via AIM to zskillz
worked great.. thanks for the help with that!

-Z
zskillz is offline   Reply With Quote
Old September 26th, 2002, 07:29 PM     #7 (permalink)
Ultimate Member
 
strangerstill's Avatar
 
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!
strangerstill is offline   Reply With Quote
Old September 26th, 2002, 08:39 PM     #8 (permalink)
Senior Member
 
Join Date: Oct 2001
Posts: 881
Send a message via AIM to zskillz
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
zskillz is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Is It Just Me? (3045)
Breaking: San Diego ACORN Document .. (10)
Delete an OS (15)
The disrespect of Obama by Russian .. (47)
Nvidia GTX 260 problem (7)
Laptop with wireless problem. (12)
Wireless Televisions. (12)
CPU fan stops spinning randomly (11)
windows vista security holes (17)
Regular Build (11)
Internet Lost (5)
windows 7 problem (7)
Point and Shoot Camera Suggestions. (6)
Is the PSU I received dead? (13)
Recent Discussions
Can't open Word (12)
Nvidia GTX 260 problem (7)
[F@H SPAM 11/16/09] ! 1/2 months to r.. (37)
Delete an OS (15)
Steam ID's, Gamertags etc... (4)
Games, Cables, PCI cards, and more fo.. (6)
Dept. of HS: NSA 'Helped' Develop Vis.. (17)
Linksys WMP54GS wireless card problem.. (5)
windows vista security holes (17)
Help getting around port 80 for camer.. (5)
Skillsoft Network+ Study Software Que.. (10)
Browsers wont load websites (3)
help me pls laptop just stopped worki.. (0)
Open With ..... Win7 (3)
Laptop with wireless problem. (12)
Internet Lost (5)
virus blocking exe. files (1)
Point and Shoot Camera Suggestions. (6)
CPU fan stops spinning randomly (11)
Modern Warfare 2: Who Bought It? (65)
Is the PSU I received dead? (13)
Print spooler problem (16)
Kingston Bluetooth Dongle Driver (1)
Multiple Restarts Required at Boot (3)
webcam (0)


All times are GMT -4. The time now is 08:33 PM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28