Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > String processing question - char set related

Reply
Thread Tools

String processing question - char set related

 
 
XXX
Guest
Posts: n/a
 
      11-22-2006
If I have a string with \r\n & I am trying to convert all \r\n to \n,
then is code like this good enough?


String s // contains the original string.

StringBuffer old = new StringBuffer(s);
StringBuffer new= new StringBuffer();

for (int i=0; i < old.length(); ++i)
{
if (strBuf.charAt(i) != '\r')
{
new.append(old.charAt(i));
}
}

Or are there problems with this? I am think of problems
like
- Is it possible to have strings with just \r not followed by \n.
When can this happen?

- Is it possible for some Unicode chars to have the \r\n pattern
which doesn't represent a new line?




 
Reply With Quote
 
 
 
 
Chris Smith
Guest
Posts: n/a
 
      11-22-2006
XXX <> wrote:
> Or are there problems with this? I am think of problems
> like
> - Is it possible to have strings with just \r not followed by \n.
> When can this happen?


Yes. There are platforms where \r is the standard representation of
end-of-line. If you need to handle end of line sequences across a
number of common platforms, then it would be safer to wrap a
StringReader with a BufferedReader, and then use readLine to get the
lines.

Alternatively, you may be reading from some protocol where the end of
line sequence is specified; for example, it's required to be \r\n for
many common internet application protocols. Then you could just look
for that one sequence and replace it with \n if that's what you want.

> - Is it possible for some Unicode chars to have the \r\n pattern
> which doesn't represent a new line?


It's safe to assume that \r\n indicates a newline whenever you find it.

--
Chris Smith
 
Reply With Quote
 
 
 
 
XXX
Guest
Posts: n/a
 
      11-22-2006
Chris Smith wrote:
> XXX <> wrote:
>> Or are there problems with this? I am think of problems
>> like
>> - Is it possible to have strings with just \r not followed by \n.
>> When can this happen?

>
> Yes. There are platforms where \r is the standard representation of
> end-of-line. If you need to handle end of line sequences across a
> number of common platforms, then it would be safer to wrap a
> StringReader with a BufferedReader, and then use readLine to get the
> lines.
>
> Alternatively, you may be reading from some protocol where the end of
> line sequence is specified; for example, it's required to be \r\n for
> many common internet application protocols. Then you could just look
> for that one sequence and replace it with \n if that's what you want.


This is going to be text, I get from a AWT TextArea widget by calling
getText()

Are these issues relavant in this case?

>> - Is it possible for some Unicode chars to have the \r\n pattern
>> which doesn't represent a new line?

>
> It's safe to assume that \r\n indicates a newline whenever you find
> it.




 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      11-22-2006

XXX wrote:
> Chris Smith wrote:
> > XXX <> wrote:
> >> Or are there problems with this? I am think of problems
> >> like
> >> - Is it possible to have strings with just \r not followed by \n.
> >> When can this happen?

> >
> > Yes. There are platforms where \r is the standard representation of
> > end-of-line. If you need to handle end of line sequences across a
> > number of common platforms, then it would be safer to wrap a
> > StringReader with a BufferedReader, and then use readLine to get the
> > lines.
> >
> > Alternatively, you may be reading from some protocol where the end of
> > line sequence is specified; for example, it's required to be \r\n for
> > many common internet application protocols. Then you could just look
> > for that one sequence and replace it with \n if that's what you want.

>
> This is going to be text, I get from a AWT TextArea widget by calling
> getText()
>
> Are these issues relavant in this case?
>
> >> - Is it possible for some Unicode chars to have the \r\n pattern
> >> which doesn't represent a new line?

> >
> > It's safe to assume that \r\n indicates a newline whenever you find
> > it.


I would bet that you don't need to worry about it in this case. A few
simple tests will let you know.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
char *fred; char * fred; char *fred; any difference? Ben Pfaff C Programming 5 01-17-2004 07:37 PM
The difference between char a[6] and char *p=new char[6] ? wwj C Programming 24 11-07-2003 05:27 PM
the difference between char a[6] and char *p=new char[6] . wwj C++ 7 11-05-2003 12:59 AM



Advertisments
 



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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57