Account 新西兰银行账户格式显示
我将在报告中显示新西兰和澳大利亚的银行账户,按照用户提供的自定义格式进行格式化。例如,新西兰银行账户可以是00-0000-0000000-000,最后一位数字(银行账户后缀)是可选的。该格式分为两部分:Account 新西兰银行账户格式显示,account,string.format,bank,Account,String.format,Bank,我将在报告中显示新西兰和澳大利亚的银行账户,按照用户提供的自定义格式进行格式化。例如,新西兰银行账户可以是00-0000-0000000-000,最后一位数字(银行账户后缀)是可选的。该格式分为两部分: 破折号的放置 2位前缀 样本格式的银行账户可以是01-1234-1234567-55和01-1234-1234567-002。银行账户存储在数据库中,没有任何格式。当我尝试String.Format(“{0:00-0000-0000000-00#}”,121234123456712)时,它不
- 破折号的放置
- 2位前缀
// ensure there is a format to use and a bank account is present
if (bankaccountformat != "" && bankaccountformat.Contains('-') && bankaccount != "")
{
int i = 0;
foreach (char dash in bankaccountformat)
{
// add dash in bank account, if bank account is long enough
if (dash == '-' && bankaccount.Length > i)
{
bankaccount = bankaccount.Insert(i, "-");
}
i++;
}
}
问题不在于帐户是否有效,而在于格式。请告诉我一种更好的方式来格式化帐户。您没有提到您使用的编程语言。不管怎样,看起来你使用了一个数组。这可能很棘手 在BASH中,我将使用字符串操作,就像在这个脚本(fmat)中一样: fmat 0112412356755给出了01-1234-1234567-55 fmat 0112341234567002给出了01-1234-1234567-002 fmat 01124123567给出了01-1234-1234567
如果您使用的不是BASH,那么至少这提供了一些提示。我忘了提到我正在使用C#哦,C#中的字符串操作也很容易。我不使用C#,所以我不能提供脚本,但我希望你能理解。
#!/bin/bash
#only numbers allowed
[[ ! "$1" =~ ^[0-9]+$ ]] && echo "Error: enter numbers only" && exit
s=$1
l=${#s}
r=l-13
#minimal number of digits is 13, maximum 16
[[ r -lt 0 || r -gt 3 ]] && echo "Error: enter 13-16 digits" && exit
#first 13 digits
a=${s:0:2}"-"${s:2:4}"-"${s:6:7}
#when 14-16 digits
a=$a"-"${s:13:r}
echo $a