SoFunction
Updated on 2025-03-07

【C# Basics】Summary of Substring's method of intercepting strings (recommended)

Preface

In the company's library project, the project is in Java; recently, in another project of the company, it needs to intercept strings, one environment is C#, and the other environment is SQL Server stored procedures. Let's talk about the method of intercepting strings in the background program.

text

The main function of intercepting strings in C# is to use the Substring function.

string (int startIndex,int length);

illustrate:

If the passed parameter is two long parameters, the first parameter refers to the starting position of the substring, that is, the position where the intercept is started, and the second parameter refers to the length of the intercept.

string (int startIndex);

illustrate:

If the passed parameter is a long integer greater than or equal to 0, the position of the long integer is used as the starting position and all the remaining strings are intercepted as substrings.

Code Example

using System;
using ;
using ;
using ;
using ;

namespace ConsoleApplication1
{
  class SubStringTest
  {
     static void Main(string[] args) 
     {
       string oriString = "Hello,Kitty!"; //Original string       string subString = ""; //Intercepted string       //Output: He       subString = (0, 2);  //Start from the first character and intercept 2 characters       (subString); 
       //Output: llo       subString = (2,3);  //Start from the first ‘l’ and intercept 3 characters       (subString); 
       //Output: Kitty!       subString = (6);  //Search from the seventh character to the end of the original string       (subString); 
     } 
  }
}

The above is a detailed explanation and integration of the Substring intercepting string method introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!