HOWTO · Csharp
Recherche de la chaîne la plus à gauche en C#
Dans ce guide, nous allons apprendre à trouver la chaîne la plus à gauche en C#.
Ce guide apprendra à trouver la chaîne la plus à gauche en C#. Comment renvoyons-nous la sous-chaîne la plus à gauche de la chaîne ?
Bien sûr, nous devrons fixer une limite, mais comment fait-on cela ? Plongeons-nous et apprenons tout à ce sujet.
Utilisez la méthode Substring(0,0) pour trouver la chaîne la plus à gauche en C#
En C#, nous pouvons utiliser le myString.Substring(0,0); méthode. Les deux paramètres sont la plage de la chaîne que vous souhaitez renvoyer.
Par exemple, si vous souhaitez renvoyer la chaîne la plus à gauche, vous devez donner une plage de 0 à 1, myString.Substring(0,1). Jetez un oeil au code suivant.
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));
}
}
}
Production:
LeftMost Character Of String Is :::: M
Dans le code ci-dessus, nous avons créé la fonction Left. À l’intérieur de cette fonction, nous avons passé la chaîne, attaché la méthode Substring(0,0) et renvoyé la plage la plus à gauche.
C’est assez simple, et c’est ainsi que vous trouvez une chaîne la plus à gauche.