简体   繁体   English

如何将字符串格式化为数字(123)456-7890

[英]How can I format a string as a number (123) 456-7890

I have a Caller.class which has a number stored as "1234567890," and I want the label lblCallbackNumber to be formatted as "(123) 456-789". 我有一个Caller.class,其数字存储为“ 1234567890”,并且我希望标签lblCallbackNumber的格式为“((123)456-789)”。 This seems to be more complicated than I had though, and most web searches are showing people how to re-format a phone number as a regular number (the opposite of what I want) 这似乎比我要复杂得多,并且大多数网络搜索都在向人们展示如何将电话号码重新格式化为常规号码(与我想要的相反)

Any help is greatly appreciated! 任何帮助是极大的赞赏!

package SupportTool;

import javafx.scene.control.*;

import java.io.*;

public class mainCallController extends Main {

    public Label lblAccount;
    public Label lblCallbackNumber;
    public Label lblCallerName;
    public Label lblStoreNumber;

    public void initialize(){

        // LOAD CALLER INFORMATION
        Caller caller = new Caller();
        try{
            FileInputStream fis = new FileInputStream("caller.bin");
            ObjectInputStream ois = new ObjectInputStream(fis);
            caller = (Caller) ois.readObject();
            ois.close();
            fis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // SET LABELS TO CALLER DETAILS
        lblAccount.setText(caller.getAccount());
        String numberAsString = caller.getCallbackNumber();
        lblCallbackNumber.setText(phoneFormat(numberAsString));
        lblCallerName.setText(caller.getCallerName());
        lblStoreNumber.setText(caller.getStoreNumber());

    }

    private String phoneFormat (String number){
        if(number.length() == 10) {

            // TODO RETURN numberAsString FORMATTED AS "(123) 456-7890"

        } else {
            return number;
        }

    }

}
String s = "1234567890";

StringBuilder sb = new StringBuilder();
sb.append("(").append(s.substring(0,3)).append(") ").append(s.substring(3,6))
        .append("-").append(s.substring(6,9));
sb.toString();

You can use regexp for your task. 您可以将regexp用于任务。 Something like this 像这样

number = number.replaceFirst("(\\d{3})(\\d{3})(\\d{4})", "($1) $2-$3");

This code will split your phone number onto 3 blocks (3 digits, 3 digits, 4 digits) and place them in order of the pattern "($1) $2-$3" 此代码会将您的电话号码分为3个块(3位,3位,4位),并按照"($1) $2-$3"顺序排列

The other way is to use StringBuilder to insert brackets and space 另一种方法是使用StringBuilder插入方括号和空格

StringBuilder builder = new StringBuilder(number)
                            .insert(0,"(")
                            .insert(4,") ")
                            .insert(8,"-");

But in my opinin regexp is more usefull and clear. 但是在我的视黄蛋白中,正则表达式更加有用和清晰。

I just answered my own question with the discovery of .substring. 我只是通过发现.substring回答了自己的问题。 It appears this is a very basic answer. 看来这是一个非常基本的答案。 Thanks for the help, as I just started learning java only 2 weeks ago. 感谢您的帮助,因为我仅在2周前才开始学习Java。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM