replace text in a string


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: replace text in a string

  1. #1
    Join Date
    Feb 2005
    Posts
    4

    replace text in a string

    All I want to do is search for "\\n" and replace it with "\n". However there appears to be no simple way to do this - am I missing something or am I going to have to write some kind of complex if then statement etc. to do this.

    Currently I was trying to use the java.lang.replace method, but it appears you can only search/change one character at a time w/ this, as I'm getting an error when I run:

    String Ntext = "Here is a newline \\n newline";
    String Rtext = Ntext.replace('\\n','\n');

  2. #2
    Join Date
    Aug 2005
    Posts
    17
    ryland22,

    The following code shows how to replace \\n with \n.

    Code:
    String text = "Hello,\\nHow are you today?";
    String newText = text.replaceAll("\\\\n","\n");
    		
    System.out.println (newText);
    The reason for the \\\\n is because the String.replaceAll method creates a regular expression and the \ has special meaning there. So you have to escape once for the regular expressions and once again for Java. So instead of writing \\n you must write \\\\n. Enjoy!

    For more help, www.NeedProgrammingHelp.com

Similar Threads

  1. Replies: 5
    Last Post: 05-27-2008, 11:17 AM
  2. Packed Data(Comp-3, etc)
    By Marcos in forum VB Classic
    Replies: 3
    Last Post: 01-25-2006, 11:18 AM
  3. verify local admin
    By Patrick Comeau in forum VB Classic
    Replies: 6
    Last Post: 03-22-2001, 10:50 PM
  4. How do I detect an FTP timeout?
    By Julian Milano in forum VB Classic
    Replies: 2
    Last Post: 08-11-2000, 12:11 PM
  5. How do I detect an FTP timeout?
    By Julian Milano in forum VB Classic
    Replies: 0
    Last Post: 08-10-2000, 09:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center