Obtains the substring at the specified position in the string.

Source: Internet
Author: User
Obtain the sub-string at the specified position in the string. There are many sub-strings that can be accessed in the MIF/Mid file. Therefore, it is easier to write a function. The test passed in VB6 and Delphi7.

' Obtain the substring at the specified position in the string, VB6
'
S: string; substr: the specified separator (one character); II: The specified position; getsubstr: The obtained character string
'
Eg: getsubstr ("123,456,789,123", 3) returns a string of 789
Public   Function Getsubstr (S As   String , Substr As   String , II As   Integer ) As   String
Dim Jj As   Integer , Tempint As   Integer
If   Instr ( 1 , S, substr, vbtextcompare) =   0   Then
Getsubstr
=   ""
Exit   Function
End   If
Tempint
=   1
For Jj =   1   To II
If   Instr (Tempint, S, substr, vbtextcompare) =   0   Then
If Jj < II Then
Getsubstr
=   ""
Else
Getsubstr
=   Mid (S, tempint, Len (S) - Tempint +   1 )
End   If
Exit   Function
End   If
Getsubstr
=   Mid (S, tempint, Instr (Tempint, S, substr, vbtextcompare) - Tempint)
Tempint
=   Instr (Tempint, S, substr, vbtextcompare) +   1
Next Jj
End Function

// Obtain the sub-string in the string, Delphi7
// S: string; substr: the specified separator (one character); index: the specified position; getsubstr: The obtained character string
// Eg: getsubstr ('123', ',', 3) returns the string 123,456,789,123
Function getsubstr (s: String ; Substr: Char; index: integer ): String ;
VaR
Tempstr:
String ;
I: integer;
Begin
Tempstr:
= S;
If POs (substr, S) = 0 Then result: =   ''
Else Begin
Try
For I: =   1 To Index -   1   Do Begin
If POs (substr, tempstr) =   0 Then begin
Result:
=   '' ;
Exit;
End;
Delete (tempstr,
1 , Pos (substr, tempstr ));
Tempstr:
= Trim (tempstr );
End;
Except
Showmessage (
' Have not such index! ' );
Result:
=   '' ;
Exit;
End;
If POs (substr, tempstr) = 0 Then
Result:
= Tempstr
Else
Result:
= Copy (tempstr, 1 , Pos (substr, tempstr) -   1 );
End;
End;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.