Narendra Verma
Narendra Verma

Reputation: 2422

Java: How to convert string into given custom format?

I want to write a java API which excepts two input parameters. First inputStr and second strFormat.

public String covertString(String inputStr, String strFormat)
{
  // Need logic
}

For example,

Input Arguments- inputStr: 999999999, strFormat: xxx-xx-xxxx
Output :  999-99-9999

Input Arguments- inputStr: 1112223333, strFormat: (xxx) xxx-xxxx
Output :  (111) 222-3333

Please suggest if there are any utilities available? If not, best way to implement this problem?

Upvotes: 2

Views: 2244

Answers (6)

user2963757
user2963757

Reputation: 107

Use string.matches and filter your criteria with regex.

Like this -----

import java.util.Scanner;

public class SocialSecurityNumber {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    while (true) {
        System.out
                .println("Input Social security Number  (accepted form 123-45-6789): ");
        String s = input.nextLine();
        if (s.matches("\\d{3}-\\d{2}-\\d{4}")) {
            System.out.println("SSN --- valid.");
            break;
        } else
            System.out.println("SSN --- not valid.");
    }

    input.close();
}

}

Upvotes: 0

JDGuide
JDGuide

Reputation: 6525

Try this:-

import javax.swing.text.MaskFormatter;


String inputStr="11122288988";
String strFormat="(###) ###-#####";
public String covertString(String inputStr, String strFormat)
{
    MaskFormatter maskFormatter= new MaskFormatter(strFormat);
    maskFormatter.setValueContainsLiteralCharacters(false);
    String finaldata=maskFormatter.valueToString(inputStr) ;
    return finaldata;
}

Output:-

Input data :- 11122288988
Formatted Data :- (111) 222-88988

Upvotes: 3

Andynedine
Andynedine

Reputation: 441

You can get your ouput with a specific pattern:

^(.{3})(.{3})(.{3})$ --> $1-$2-$3 to get 999-999-999 from 999999999
^(.{3})(.{3})(.{3})$ --> ($1) $2-$3 to get (999) 999-999 from 999999999

public String covertString(String inputStr, String strInputPattern, String strOutputPattern)
{
    //strInputPattern could be "^(.{3})(.{3})(.{3})$"
    //strOutputPattern could be "$1-$2-$3"
    return inputStr.replaceAll(strInputPattern, strOutputPattern)
}

I hope this help you

Upvotes: 0

M.Raheel
M.Raheel

Reputation: 165

the second parameter String strFormat you should implement the ASCII character of (-)and () in your logic .

I think this will help :http://javarevisited.blogspot.com/2012/08/how-to-format-string-in-java-printf.html

Upvotes: 0

fge
fge

Reputation: 121720

This should work, but assumes that the input string is the correct length etc. If such checks are to be implemented, are left to the OP to implement:

public String covertString(String inputStr, String strFormat)
{
    final char[] array = strFormat.toCharArray(); // dups the content
    int inputIndex = 0;

    for (int index = 0; index < array.length; index++)
        if (array[index] == 'x')
            array[index] = inputStr.charAt(inputIndex++);

    return new String(array);
}

Upvotes: 1

StackExploded
StackExploded

Reputation: 526

you go through strFormat characters 1 by 1, if you meet "x", you write digit from inputStr, else you write the character

Upvotes: 2

Related Questions