Home  >  Article  >  Backend Development  >  Substring in C#

Substring in C#

WBOY
WBOYforward
2023-09-16 23:01:06924browse

C# 中的子字符串

Substring is used to get the subpart of a string in C#. For this we have substring() method. Check if each substring has unique characters using the substring() method in C#. Loop it until the length of the string.

If any of the substrings matches another substring, it means that the string has no unique characters.

You can try running the following command to determine if a string contains the codes for all unique characters. This example shows the usage of Substring() method -

Example

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

public class Demo {

   public bool CheckUnique(string str) {
      string one = "";
      string two = "";

      for (int i = 0; i < str.Length; i++) {
         one = str.Substring(i, 1);
         for (int j = 0; j < str.Length; j++) {
            two = str.Substring(j, 1);
            if ((one == two) && (i != j))
            return false;
         }
      }
      return true;
   }

   static void Main(string[] args) {
      Demo d = new Demo();
      bool b = d.CheckUnique("amit");
      Console.WriteLine(b);

      Console.ReadKey();
   }
}

The above is the detailed content of Substring in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete