Skip to Content
author's profile photo Former Member
Former Member

Simple UDF question.

My requirement is that I need to seperate out all the special characters in a string and take only the numbers.

If phone number contains any brackets or hyphens, they need to be removed to form only the number.

Since my datatype is string type.. I needed to use the string argument for my function.

Please can anyone tell me how do I achieve this?.

I was looking for a for loop to check the charAt every position.. but how do I seperate the chars of a string to form a new one?.

Please advise.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

2 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Nov 07, 2008 at 12:47 AM

    Hi

    If you need to do it only for Numbers then use this in UDF

    public String removeNonDigits(String text, Container container) {
    		int length = text.length();
    		StringBuffer buffer = new StringBuffer(length);
    		for(int i = 0; i < length; i++) {
    			char ch = text.charAt(i);
    			if (Character.isDigit(ch)) {
    				buffer.append(ch);
    			}
    		}
    		return buffer.toString();
    	}

    Thanks

    Gaurav

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Nov 07, 2008 at 06:27 PM

    Thanks Gaurav.

    I was trying with Arraylist..and if and else for all the digits from 1 to 9.

    But the character.isdigit function did it for me.

    Thanks again.

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.