I do not know if this is the right place for this question, but I
think so
I want to write two regular expression which is:
String st = str.replaceAll("\\n", "").replaceAll("\\r", "");
Can anybody help me with this little question?
Lew - 05 Feb 2010 22:43 GMT
> I do not know if this is the right place for this question, but I
> think so
> I want to write two regular expression which is:
> String st = str.replaceAll("\\n", "").replaceAll("\\r", "");
>
> Can anybody help me with this little question?
String st = str.replaceAll( "[\\n\\r]", "" );

Signature
Lew
The87Boy - 05 Feb 2010 22:50 GMT
> > I do not know if this is the right place for this question, but I
> > think so
[quoted text clipped - 4 lines]
>
> String st = str.replaceAll( "[\\n\\r]", "" );
Thx for the help Lew
Roedy Green - 06 Feb 2010 07:34 GMT
>I do not know if this is the right place for this question, but I
>think so
>I want to write two regular expression which is:
>String st = str.replaceAll("\\n", "").replaceAll("\\r", "");
>
>Can anybody help me with this little question?
Are you looking for code that will efficiently remove all \n and \r
characters in a string?
A flat-footed loop like this will do it faster than any regex.
private static string unwrap ( String s )
{
if ( s.indexOf( '\n' ) < 0 && s.indexOf( '\r' ) < 0 )
{
return s;
}
StringBuilder sb = new StringBuilder( s.length() );
for ( int i=0; i<s.length(); i++ )
{
char c = s.charAt( i );
if ( !( c == '\n' || c == '\r' ) )
{
sb.append( c );
}
}
return s.toString();
}
I think however, you actually probably want to replace \r\n with
space.
if ( c == '\n' || c == '\r' )
{
sb.append( ' ' );
}
else
{
sb.append( c );
}

Signature
Roedy Green Canadian Mind Products
http://mindprod.com
You cant have great software without a great team, and most software teams behave like dysfunctional families.
~ Jim McCarthy
Roedy Green - 06 Feb 2010 07:38 GMT
>I do not know if this is the right place for this question, but I
>think so
>I want to write two regular expression which is:
>String st = str.replaceAll("\\n", "").replaceAll("\\r", "");
>
>Can anybody help me with this little question?
If you want to use regexes, and replace different elements with
different expressions, you need to use
Matcher.appendReplacement and a StringBuffer.

Signature
Roedy Green Canadian Mind Products
http://mindprod.com
You cant have great software without a great team, and most software teams behave like dysfunctional families.
~ Jim McCarthy
Roedy Green - 06 Feb 2010 19:55 GMT
>String st = str.replaceAll("\\n", "").replaceAll("\\r", "");
to answer your question literally, all you need is a regex expression
that will match \n or \r as your first argument. You could do that
with
"\\n|\\r" or "[\\n\\r]"

Signature
Roedy Green Canadian Mind Products
http://mindprod.com
Every compilable program in a sense works. The problem is with your unrealistic expections on what it will do.