772 snippets from 1557 members, and growing!
|
login
|
join
about
bytebin
members
tags
snippets
join
Snippets
Submit a Snippet
Search Snippets
New Snippets
Top Snippets
Top Tags
PHP
(136)
JavaScript
(123)
Java
(66)
VBSCRIPT
(58)
String
(44)
CSS
(31)
CSharp
(28)
File
(28)
HTML
(27)
C
(24)
mysql
(24)
VB.NET
(24)
python
(24)
CPlusPlus
(23)
groovy
(23)
New Snippets
Consumer Complain...
Private Snippet?
Very lightweight ...
AutoComplete plug...
AutoComplete plug...
Connection Java -...
View PostgreSql
Store Procedure
Pygame - Simple p...
Python - Anagram ...
Venture Capital Jobs
New Members
kazumaniax
bmax1368
me
jamesmcm
Can
Kelmi
ysg
dannymo2
chorny
wallie
Top Members
dannyboy
sundaramkumar
mattrmiller
Pio
i_kenneth
ASmith
ctiggerf
sehrgut
bertheymans
SCoon
Home
/
Snippets
/
Chop Newlines
/
Comments
Chop Newlines
Snippet Menu
Revisions
Comments
Related Snippets
Add to Favorites
Email Snippet
Download Snippet
Print Snippet
Blog Snippet
snippet
|
revisions
|
comments
|
related
|
Add to Favorites
|
email
download
|
print
|
blog it
New Comment
Unix/Mac file format
Sat. Oct. 28th, 2006 2:17 PM
SCoon
This implementation will throws an exception on unix-style text files. And it may produce very strange results on Mac-style text files.
Reply
Optimization
Fri. Sep. 22nd, 2006 8:15 PM
Kivanc_Anar
Let me optimize your code that way
public
static
String
chopNewline
(
String
str
)
{
return
(
str.
replaceAll
(
"
\r
"
,
""
)
.
replace
(
"
\n
"
,
""
)
)
;
}
Reply
DOS/Windows only
Sat. Oct. 28th, 2006 2:19 PM
SCoon
This code can be applied
only
to the wellformed DOS/Windows text files.
Reply
Isn't this much simpler?
Wed. Mar. 15th, 2006 6:12 AM
TimYates
That does seem like a lot of work, when replaceAll can do it all for you
// Chop new lines from anywhere in a String
public
static
String
chopAllNewlines
(
String
str
)
{
return
str.
replaceAll
(
"
\n
|
\r
"
,
""
)
;
}
// Chop new lines from just at the end of a String
public
static
String
chopFinalNewline
(
String
str
)
{
return
str.
replaceAll
(
"
\n
$|
\r
$"
,
""
)
;
}
Reply
New Comment
public static String chopNewline(String str) {
return(str.replaceAll("\r","").replace("\n",""));
}
// Chop new lines from anywhere in a String
public static String chopAllNewlines(String str)
{
return str.replaceAll( "\n|\r", "" ) ;
}
// Chop new lines from just at the end of a String
public static String chopFinalNewline(String str)
{
return str.replaceAll( "\n$|\r$", "" ) ;
}