Maison  >  Article  >  développement back-end  >  Propriétés de la chaîne C#

Propriétés de la chaîne C#

PHPz
PHPzavant
2023-09-15 19:25:02822parcourir

C# 字符串属性

Voici les propriétés de la classe String en C# -

Chars2LengthExempleVoyons un exemple- Démonstration en directVoyons maintenant un autre exemple- Démonstration en direct
Sr

Obtenir l'objet String actuel Char à la position spécifiée dans .

Obtenir le nombre de caractères dans l'objet String actuel.

using System;
public class Demo {
   public static void Main() {
      string str1 = "h8b9";
      string str2 = "abcdef";
      Console.WriteLine("Is string1 null or empty? = "+string.IsNullOrEmpty(str1));
      Console.WriteLine("Is string2 null or empty? = "+string.IsNullOrEmpty(str2));
      bool val = str1 != str2;
      Console.WriteLine("Is str1 equal to str2? = "+val);
      for (int i = 0; i < str1.Length; i++) {
         if (Char.IsLetter(str1[i]))
            Console.WriteLine(""+str1[i]+" is a letter");
         else
            Console.WriteLine(""+str1[i]+" is a number");
      }
   }
}
SortieCela produira la sortie suivante-

Is string1 null or empty? = False
Is string2 null or empty? = False
Is str1 equal to str2? = True
h is a letter
8 is a number
b is a letter
9 is a number

Exemple

using System;
public class Demo {
   public static void Main() {
      string str1 = "hijklm";
      string str2 = String.Empty;
      Console.WriteLine("Is string1 null or whitespace? = "+String.IsNullOrWhiteSpace(str1));
      Console.WriteLine("Is string2 null or whitespace? = "+String.IsNullOrWhiteSpace(str2));
      Console.WriteLine("String1 length = "+str1.Length);
      Console.WriteLine("String2 length = "+str1.Length);
      Console.WriteLine("String length = "+"demo".Length);
   }
}

Output

Cela produira l'exemple suivant -

Is string1 null or whitespace? = False
Is string2 null or whitespace? = True
String1 length = 6
String2 length = 6
String length = 4

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Cet article est reproduit dans:. en cas de violation, veuillez contacter admin@php.cn Supprimer