2014-09-04 2 views
0

studentDto가 있습니다. 성에 대해 문자 수인 을 확인하고 싶습니다. 문자 수는 8보다 큰 경우에, 나는 두 개의 별표 따라서 다른 문자 문자열을 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 { } 
    } 
} 
+0

는' –

+0

라이브러리가 당신을 위해 더 많은 일을하자 String.Substring'. 문자열 클래스는 IndexOf를 찾거나 SubString을 가져 오는 훌륭한 방법을 제공합니다. Some like like LastName.Length> 8 : LastName.SubString (0,7) + "**": LastName –

+0

"var checkLength = LastName.Length;"를 호출 할 때 순환 루프가있는 것처럼 보입니다. 그것은 현재있는 LastName과 동일한 get을 반복해서 실행할 것입니다. – ingo

답변

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; 
    } 
} 
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; } 
    } 

한 것은 아마도 성 속성 게터의 성의 사용, 큰 아니오 아니오, 그 원인이 재귀하고있다 이보다 간결하게 작성 될 수있는 스택 오버 플로우 예외

을 얻고있다,하지만 당신

+0

라이브러리 함수는 괜찮습니다. 괜찮아. 어떻게해야합니까? – user2320476

+0

스티브에게 감사드립니다. getter에 넣는 것보다 이것을 넣을 적절한 장소는 어디입니까? 아마 하나를 사용하기 때문에 저장 프로 시저 수준에서이 작업을 수행 할 수 있습니다. – user2320476

+0

내가 의미했던 것은 재귀 적이며 스택 오버플로가 발생할 LastName 속성에서 LastName 속성에 액세스하고 있다는 의미였습니다. 그 이유는 _lastName 문자열을 추가하고 그것을 사용하여 뒷받침 속성으로 변경 한 이유입니다. – Steve

0

Linq의 방법 "., 많은 * 필요와 다음 패드를 8 개 문자를 가지고이 배열에서 새 캐릭터를 생성"

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

완전한 이행 : 당신은 보길 원하는 것일 수도

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; 
    } 
} 
관련 문제