C#에서 맨 왼쪽 문자열 찾기

Haider Ali 2023년10월12일
C#에서 맨 왼쪽 문자열 찾기

이 가이드에서는 C#에서 가장 왼쪽에 있는 문자열을 찾는 방법을 배웁니다. 문자열의 가장 왼쪽 부분 문자열을 어떻게 반환합니까?

물론 경계를 설정해야 하지만 어떻게 해야 할까요? 그것에 대해 모든 것을 배워봅시다.

Substring(0,0) 메서드를 사용하여 C#에서 가장 왼쪽 문자열 찾기

C#에서는 myString.Substring(0,0);을 사용할 수 있습니다. 방법. 두 매개변수는 반환하려는 문자열의 범위입니다.

예를 들어, 가장 왼쪽에 있는 문자열을 반환하려면 0에서 1 사이의 범위인 myString.Substring(0,1)을 지정해야 합니다. 다음 코드를 살펴보십시오.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace left_string {
  class Program {
    static void Main(string[] args) {
      String str = "MARK-ZUKERBERG";
      // # Problem return the leftmost characters of the string
      // with help of substring method i am writing  a left function which will return the leftmost
      // chracter
      Console.WriteLine("LeftMost Character Of String Is :::: " + str.Left());

      Console.ReadKey();
    }
  }
  public static class StringExtensions {
    public static string Left(this string value)  // current String value as a Parameter
    {
      if (string.IsNullOrEmpty(value))
        return value;  // if String is an empty String.

      return (value.Substring(0, 1));
    }
  }
}

출력:

LeftMost Character Of String Is :::: M

위의 코드에서 Left 함수를 만들었습니다. 해당 함수 내에서 문자열을 전달하고 Substring(0,0) 메서드를 연결하고 가장 왼쪽 범위를 반환했습니다.

그것은 매우 간단하며 이것이 가장 왼쪽 문자열을 찾는 방법입니다.

작가: Haider Ali
Haider Ali avatar Haider Ali avatar

Haider specializes in technical writing. He has a solid background in computer science that allows him to create engaging, original, and compelling technical tutorials. In his free time, he enjoys adding new skills to his repertoire and watching Netflix.

LinkedIn

관련 문장 - Csharp String