簡體   English   中英

如何格式化數字以使前6位和后4位不隱藏

[英]How to format a number such that first 6 digits and last 4 digits are not hidden

如何格式化數字以使前6位和后4位不隱藏

這樣111111111111111看起來像111111 **** 1111

一種簡單的方法是分割輸入。

int number = 111111111111111;

string firstsix  =  number.ToString().Substring(0,6) //gets the first 6 digits
string middlefour = number.ToString().Substring(6,4) //gets the next 4
string rest = number.ToString().Substring(10, number.ToString().Lenght) //gets the rest

string combined = firstsix  + "****" +rest;

您也可以使用LINQ,用索引大於5且小於number.Length - 4 char替換number.Length - 4 * number.Length - 4*

string number = "111111111111111";

string res = new string(number.Select((c, index) => { return (index <= 5 || index >= number.Length - 4) ? c : '*'; }).ToArray());

Console.WriteLine(res); // 111111*****1111

您需要按順序使用\\G錨來進行連續的字符串匹配。

string result = Regex.Replace(str, @"(?:^(.{6})|\G).(?=.{4})", "$1*");

演示

愛迪生

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM