Analysis on the replacement of strings implemented by the mid Function

Source: Internet
Author: User
Analysis on the replacement of strings implemented by the mid Function

The mid function is generally used to return the neutron string of the original string, for example

Dim mystring, firstword, lastword, midwords

Mystring = "mid function Demo" creates a string.

Firstword = mid (mystring, 1, 3) 'returns "mid ".

Lastword = mid (mystring, 14, 4) 'returns "Demo ".

Midwords = mid (mystring, 5) 'returns "funcion

 

The specific functions are described as follows:

ReturnVariant(String), Which contains the specified number of characters in the string.

Syntax

Mid(String,Start[,Length])

MidThe function syntax has the following naming parameters:

Part

Description

String

Required parameter. String expression, which returns characters. IfStringContains null.Null.

Start

Required parameter. Long.StringThe character location of the extracted part. If StartExceedsStringNumber of characters,MidReturns a zero-length string ("").

Length

Optional;Variant(Long). Number of characters to return.If omitted orLengthExceeds the number of characters in the text (includingStartAnd returns the string fromStartAll characters to the end.

 

Description

Want to knowStringNumber of characters, availableLenFunction.

Note: MidbThe function acts on the byte data contained in a string, just as in the dual-byte character set (DBCS) language. Therefore, the parameter specifies the number of bytes rather than the number of characters. ForMidbSee the second example in the example topic.

Can we use it to replace some strings? Run the following function on your machine:

Option explicit

 

Public Function Test () as string

Dim s as string

S = "ABCD"

Mid (s, 1, 1) = "T"

Test = s

End Function

What did you find? Wow, the output string is changed to "tbcd! Yes, we can use mid to replace the string (very happy )! What if I change "T" to "TX? See the following code:

Option explicit

 

Public Function Test () as string

Dim s as string

S = "ABCD"

Mid (s, 1, 1) = "TX"

Test = s

End Function

Run it again. As expected, it is still "tbcd". Although mid can replace some simple strings, its precondition is that the string cannot exceed its return length (in this example, it is 1 ). Some netizens may ask what will happen if it is smaller than the return length? Well, Let's explain the experiment results. See:

Option explicit

 

Public Function Test () as string

Dim s as string

S = "ABCD"

Mid (s, 1, 3) = "TX"

Test = s

End Function

The result is as follows: "txcd ".

From the above results, mid can be replaced with some simple strings, but the following conditions must be observed:

The maximum length of replacement is determined by the maximum length returned by mid. If the length of the replacement string is greater than the return length, only the first part of the replacement string is used for replacement. If the length of the replacement string is smaller than the return length, the excess of the original string will be retained, and the rest will be replaced. Although mid has a very clever function in some occasions, if it involves common string operations, I suggest using it as much as possible.Replace (expression, find, replace [, start [, Count [, compare]).

 

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.