• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
RE: Getting rid of Carriage returns
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Getting rid of Carriage returns


  • Subject: RE: Getting rid of Carriage returns
  • From: Andy Beier <email@hidden>
  • Date: Thu, 23 Dec 2004 13:46:17 -0700
  • Thread-topic: Getting rid of Carriage returns

Title: Getting rid of Carriage returns

I would use Java Regular expressions.

 

Here is a class I wrote that simplifies using them. Also for more pattern examples look at this java doc: http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html.

 

For what you want to do this _expression_ should work: [\\r\\n\\f]

 

import java.util.regex.*;

 

public class Regtil {

 

  private Regtil() {

  }

 

  /**

            *  Takes a string and based on a regular _expression_ replaces it with the given value.

            *

            * @param  s  The string to be changed

            * @param  p  The regular _expression_. Example: [^\\w'] replaces all non a-z, A-Z characters

            * @param  r  The String that you wish to replace the characters that match the pattern with.

            * @see Pattern

            * @return Changed string or if no changes original

            */

 

  public static String replaceReg(String s, String p, String r){

 

                Pattern pn = Pattern.compile(p);

                Matcher m = pn.matcher(s);

 

        return m.replaceAll(r);

        }

 

}

 

 

Example:

 

String s = Regtil.replaceReg(string, “[\\r\\n\\f]”, “ “);

 

 

 

Hope this helps.

 

Andy

 


From: webobjects-dev-bounces+abeier=email@hidden [mailto:webobjects-dev-bounces+abeier=email@hidden] On Behalf Of Calven Eggert
Sent: Thursday, December 23, 2004 12:47 PM
To: email@hidden
Subject: Getting rid of Carriage returns

 

Hi All,

I have text I want to export to an Excel file.  The text for a field may have carriage returns in it so I need to remove them and replace them with spaces.  I’ve used StringTokenizer and then I used the Class String replace method and they both are not working for me.

Here is my little routine:

   
public String removeReturns(String beforeStr) {
        String tempStr = "";
        StringTokenizer textTokens = new StringTokenizer(beforeStr, "\n", false);
        while(textTokens.hasMoreTokens()) {
            String aToken = (String)textTokens.nextToken();
            tempStr += aToken;
        }
        return tempStr;
    }


It doesn’t work.  It still returns my string with the carriageretuns in it.

Can anyone help?

Calven

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Prev by Date: Re: Getting rid of Carriage returns
  • Next by Date: EOEditingContext Question
  • Previous by thread: Re: Getting rid of Carriage returns
  • Next by thread: EOEditingContext Question
  • Index(es):
    • Date
    • Thread