将字符串限制为八个字符并使用星号返回

时间:2014-09-04 21:29:43

标签: c#-4.0

我有一名学生。我想确定一下这个数字 LastName的字符数。如果字符数更大 比8,我想用两个星号返回8个字符的姓氏 切断其他角色

例如Abumadem **

这是我开始的方式。我无法让它工作。你能帮忙吗?

public class StudentDto
{
    public string Firstname { get; set; }
    public string EmailAddress { get; set; }

    public string LastName
    {
        get
        {
            var checkLength = LastName.Length;
            string First8Chars = string.Empty;
            int count=0;
            List<char> storeStrings = new List<char>();

            if (checkLength > 8)
            {                        
                foreach (var c in LastName)
                {
                    storeStrings.Add(c);
                    if ()
                    {
                    }
                    count++;
                }
            }
        }
    }
}

这是新的尝试,但没有运气。

public class StudentDto
{
    public string Firstname { get; set; }
    public string EmailAddress { get; set; }

    public string LastName
    {
        get
        {
            var checkLength = LastName.Length;
            string First8Chars = string.Empty;

            if (checkLength > 8)
            {
                First8Chars = LastName.Substring(0, 7) + "**";
                return First8Chars;
            }                   
        }
        set { }
    }
}

3 个答案:

答案 0 :(得分:1)

就这样做:

string _backingFieldLastName;
public string LastName
{
    get
    {
        return _backingFieldLastName == null || backingFieldLastName.Length <=8 ? 
               _backingFieldLastName :
               _backingFieldLastName.Substring(0,8) +"**"; // second parameter of substring is count of chars from the start index (first parameter)
    }
    set
    {
       _backingFieldLastName = value;
    }
}

答案 1 :(得分:0)

如果您因某些原因无法使用库函数:

private string _lastName = "";

    public string LastName
    {
        get
            {
                var checkLength = _lastName.Length;
                string First8Chars = string.Empty;
                string storeStrings = "";

                if (checkLength > 8)
                {
                    foreach (var c in _lastName)
                    {
                        storeStrings += c;
                        if (storeStrings.Length == 8)
                        {
                            storeStrings += "**";
                            return storeStrings;
                        }
                    }

                }
                return storeStrings;
            }
        set { _lastName = value; }
    }

我注意到的一件事是你在LastName属性getter中使用LastName,这是一个很大的没有,它导致递归,你可能会得到一个堆栈溢出异常

这可以写得更简洁,但我会将其作为练习留给你

答案 2 :(得分:0)

Linq方式:

var lastname = "Abumademal";
var formatted = (new string(lastname.Take(8).ToArray())).PadRight(lastname.Length, '*');
// will yield "Abumadem**"

“取8个字符并从此数组中创建一个新字符串,然后根据需要用尽可能多的*填充它。”

完全实施:

private string lastname;

public string LastName
{
    get
    {
        if (null == this.lastname)
        {
            return null;
        }

        char[] firsteight = this.lastname.Take(8).ToArray();
        string tmp = new string(firsteight);
        // padding this way wasn't the actual requirement ...
        string result = tmp.PadRight(this.lastname.Length, '*');
        return result;
    }

    set
    {
        this.lastname = value;
    }
}