본문 바로가기

C#4

[C#] C# string.Replace 메서드 public string Replace (char oldChar, char newChar); oldChar를 newChar로 치환하여 반환한다. oldChar가 없으면 기존 값을 반환한다. 현재의 인스턴스 값은 수정되지 않는다. using System; public class Example { public static void Main() { String s = new String('a', 3); Console.WriteLine("The initial string: '{0}'", s); s = s.Replace('a', 'b').Replace('b', 'c').Replace('c', 'd'); Console.WriteLine("The final string: '{0}'", s); } } [결과] The .. 2019. 9. 26.
[C#] C# string.Substring 메서드 public string Substring (int startIndex); 인스턴스에서 startIndex부터 마지막까지의 부분 문자열을 검색한다. 현재의 인스턴스 값은 수정되지 않는다. *인덱스는 0부터 시작한다. using System; public class Example { public static void Main() { String[] pairs = { "Color1=red", "Color2=green", "Color3=blue", "Title=Code Repository" }; foreach (var pair in pairs) { int position = pair.IndexOf("="); if (position < 0) continue; Console.WriteLine("Key: {0}, .. 2019. 9. 26.
[Unity | 유니티] 커스텀 정렬 알고리즘 만들기 특정한 조건을 충족하는 정렬 알고리즘이 필요할 경우 커스텀 정렬 알고리즘을 만드는 방법을 소개한다. 기존 list.sort((a,b) => a.CompareTo(b)); //오름차순 정렬 list.sort((a,b) => b.CompareTo(a)); //내림차순 정렬 커스텀 List list = new List() {a, b, c, d}; list.sort(new SortComparer()); 12345678910111213141516171819public class SortComparer : IComparer{ public int Compare(item x, item y) { //정렬 조건을 설정한다. //item의 weight를 내림차순으로 정렬한다고 가정해보자. //x 순서가 y보다 앞이면 -1 .. 2018. 11. 21.
[Unity | 유니티] Serializable Class 활용하기 인스펙터에서 상의 변수들을 그룹으로 분류하여 serializable class로 만들면 관리가 용이하다. Serializable Class 만들기 공통 속성을 가진 변수들을 그룹지어 클래스를 만든다. [Serializable] 속성을 부여한다. 생성한 클래스에 변수들을 모두 public으로 선언한다. Awake()나 Start()에서 초기화할 필요가 없다. 초기화는 유니티가 자체적으로 해 준다. 변수 선언시 초기화 값을 지정할 수 있다. 플레이어가 가진 속성을 이동속성과 체력속성으로 분리하여 serializable class로 관리해 보았다. 123456789101112131415161718[Serializable]public class MovementProperties //Not a MonoBehavi.. 2018. 11. 21.