java phone 使用字符串生成器和拆分更改格式
java phone format change using String builder and split
我试图将字符串 phone 数字重新格式化为 +1(204)867-5309
,方法是首先拆分中间的点,然后检查其输入是否有效,如果不是 throw new IllegalArgumentException
.但出于某种原因,即使我使用 String 生成器更改它,数字的输出也没有改变。
提前致谢。
public class format{
public static void main (String[] args){
String phone = "204.867.5309";
System.out.println(format(phone));
}
public static String format(String phone){
char [] phoneLine = phone.toCharArray();
String [] split = phone.split("\.");
for (char c: phoneLine){
if (phoneLine[3]=='.'
&& phoneLine[7]=='.'
&& phoneLine.length == 12
&& Character.isDigit(phoneLine[1])
&& Character.isDigit(phoneLine[0])
&& Character.isDigit(phoneLine[2])
&& Character.isDigit(phoneLine[4])
&& Character.isDigit(phoneLine[5])
&& Character.isDigit(phoneLine[6])
&& Character.isDigit(phoneLine[8])
&& Character.isDigit(phoneLine[9]))
{
StringBuilder sb = new StringBuilder(phone);
sb.append("+1");
sb.insert(2,"(");
sb.insert(6, ")");
sb.insert(10,"-");
}
else {
throw new IllegalArgumentException("not valid");
}
}
return phone;
}
}
你能试试下面的 code.output:20(48)67-5309+1
public class PhoneNumberFormat {
public static void main(String[] args) {
String phone = "204.867.5309";
System.out.println(format(phone));
}
public static String format(String phone) {
String[] split = phone.split(".");
split = phone.split("\.");
String newPhone = "";
for (String s : split) {
newPhone = newPhone + s;
}
char[] phoneLine = newPhone.toCharArray();
StringBuilder sb = new StringBuilder();
for (char c : phoneLine) {
if (phoneLine.length == 10
&& Character.isDigit(phoneLine[1])
&& Character.isDigit(phoneLine[0])
&& Character.isDigit(phoneLine[2])
&& Character.isDigit(phoneLine[4])
&& Character.isDigit(phoneLine[5])
&& Character.isDigit(phoneLine[6])
&& Character.isDigit(phoneLine[8])
&& Character.isDigit(phoneLine[9])) {
sb = new StringBuilder(newPhone);
sb.append("+1");
sb.insert(2, "(");
sb.insert(6, ")");
sb.insert(10, "-");
} else {
throw new IllegalArgumentException("not valid");
}
}
return sb.toString();
}}
尝试使用此代码。 for 循环和字符串拆分不是必需的:
public static void main(String[] args) {
String phone = "204.867.5309";
System.out.println(format(phone));
}
public static String format(String phone) {
char[] phoneLine = phone.toCharArray();
if (phoneLine[3] == '.'
&& phoneLine[7] == '.'
&& phoneLine.length == 12
&& Character.isDigit(phoneLine[1])
&& Character.isDigit(phoneLine[0])
&& Character.isDigit(phoneLine[2])
&& Character.isDigit(phoneLine[4])
&& Character.isDigit(phoneLine[5])
&& Character.isDigit(phoneLine[6])
&& Character.isDigit(phoneLine[8])
&& Character.isDigit(phoneLine[9])) {
String[] splittedPhone = phone.split("\.");
StringBuilder sb = new StringBuilder("+1");
int i = 0;
for (String sub : splittedPhone) {
if (i == 0) {
sb = sb.append("(");
sb = sb.append(sub);
sb = sb.append(")");
} else if (i == 1) {
sb = sb.append(sub);
} else if (i == 2) {
sb = sb.append("-");
sb = sb.append(sub);
}
i++;
}
return sb.toString();
} else {
throw new IllegalArgumentException("not valid");
}
}
我试图将字符串 phone 数字重新格式化为 +1(204)867-5309
,方法是首先拆分中间的点,然后检查其输入是否有效,如果不是 throw new IllegalArgumentException
.但出于某种原因,即使我使用 String 生成器更改它,数字的输出也没有改变。
提前致谢。
public class format{
public static void main (String[] args){
String phone = "204.867.5309";
System.out.println(format(phone));
}
public static String format(String phone){
char [] phoneLine = phone.toCharArray();
String [] split = phone.split("\.");
for (char c: phoneLine){
if (phoneLine[3]=='.'
&& phoneLine[7]=='.'
&& phoneLine.length == 12
&& Character.isDigit(phoneLine[1])
&& Character.isDigit(phoneLine[0])
&& Character.isDigit(phoneLine[2])
&& Character.isDigit(phoneLine[4])
&& Character.isDigit(phoneLine[5])
&& Character.isDigit(phoneLine[6])
&& Character.isDigit(phoneLine[8])
&& Character.isDigit(phoneLine[9]))
{
StringBuilder sb = new StringBuilder(phone);
sb.append("+1");
sb.insert(2,"(");
sb.insert(6, ")");
sb.insert(10,"-");
}
else {
throw new IllegalArgumentException("not valid");
}
}
return phone;
}
}
你能试试下面的 code.output:20(48)67-5309+1
public class PhoneNumberFormat {
public static void main(String[] args) {
String phone = "204.867.5309";
System.out.println(format(phone));
}
public static String format(String phone) {
String[] split = phone.split(".");
split = phone.split("\.");
String newPhone = "";
for (String s : split) {
newPhone = newPhone + s;
}
char[] phoneLine = newPhone.toCharArray();
StringBuilder sb = new StringBuilder();
for (char c : phoneLine) {
if (phoneLine.length == 10
&& Character.isDigit(phoneLine[1])
&& Character.isDigit(phoneLine[0])
&& Character.isDigit(phoneLine[2])
&& Character.isDigit(phoneLine[4])
&& Character.isDigit(phoneLine[5])
&& Character.isDigit(phoneLine[6])
&& Character.isDigit(phoneLine[8])
&& Character.isDigit(phoneLine[9])) {
sb = new StringBuilder(newPhone);
sb.append("+1");
sb.insert(2, "(");
sb.insert(6, ")");
sb.insert(10, "-");
} else {
throw new IllegalArgumentException("not valid");
}
}
return sb.toString();
}}
尝试使用此代码。 for 循环和字符串拆分不是必需的:
public static void main(String[] args) {
String phone = "204.867.5309";
System.out.println(format(phone));
}
public static String format(String phone) {
char[] phoneLine = phone.toCharArray();
if (phoneLine[3] == '.'
&& phoneLine[7] == '.'
&& phoneLine.length == 12
&& Character.isDigit(phoneLine[1])
&& Character.isDigit(phoneLine[0])
&& Character.isDigit(phoneLine[2])
&& Character.isDigit(phoneLine[4])
&& Character.isDigit(phoneLine[5])
&& Character.isDigit(phoneLine[6])
&& Character.isDigit(phoneLine[8])
&& Character.isDigit(phoneLine[9])) {
String[] splittedPhone = phone.split("\.");
StringBuilder sb = new StringBuilder("+1");
int i = 0;
for (String sub : splittedPhone) {
if (i == 0) {
sb = sb.append("(");
sb = sb.append(sub);
sb = sb.append(")");
} else if (i == 1) {
sb = sb.append(sub);
} else if (i == 2) {
sb = sb.append("-");
sb = sb.append(sub);
}
i++;
}
return sb.toString();
} else {
throw new IllegalArgumentException("not valid");
}
}