屏蔽掉部分字符串前12个字符*? [英] Mask out part first 12 characters of string with *?

查看:107
本文介绍了屏蔽掉部分字符串前12个字符*?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何能取值 123456789012345 1234567890123456 ,并把它变成:

************ 2345 ************ 3456

上面的字符串之间的区别在于,一个含有15个数字,另一个包含16

我曾尝试以下,但它不保留的15位号码的最后四位数字,现在不管是什么字符串的长度,无论是13,14,15,或16,我想屏蔽所有开始数字用*,但保留最后4这是我曾尝试:

 的String.Format({0} {1},************,str.Substring(11,str.Length  - 12))
 

解决方案

 使用系统;

类节目
{
    静态无效的主要()
    {
        VAR海峡=1234567890123456;
        如果(str.Length→4)
        {
            Console.WriteLine(
                string.Concat(
                    .PadLeft(12,'*'),
                    str.Substring(str.Length  -  4)
                )
            );
        }
        其他
        {
            Console.WriteLine(STR);
        }
    }
}
 

How can I take the value 123456789012345 or 1234567890123456 and turn it into:

************2345 and ************3456

The difference between the strings above is that one contains 15 digits and the other contains 16.

I have tried the following, but it does not keep the last 4 digits of the 15 digit number and now matter what the length of the string, be it 13, 14, 15, or 16, I want to mask all beginning digits with a *, but keep the last 4. Here is what I have tried:

String.Format("{0}{1}", "************", str.Substring(11, str.Length - 12))

解决方案

using System;

class Program
{
    static void Main()
    {
        var str = "1234567890123456";
        if (str.Length > 4)
        {
            Console.WriteLine(
                string.Concat(
                    "".PadLeft(12, '*'), 
                    str.Substring(str.Length - 4)
                )
            );
        }
        else
        {
            Console.WriteLine(str);
        }
    }
}

这篇关于屏蔽掉部分字符串前12个字符*?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆