Substring in C#

WBOY
Release: 2023-09-16 23:01:06
forward
926 people have browsed it

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();
   }
}
Copy after login

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

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template