📄 vel13.htm
字号:
VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>CLng()</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Converts the argument to an equivalent long integer data type</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>CSng()</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Converts the argument to an equivalent single-precision data type</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>CStr()</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Converts the argument to an equivalent string data type</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>CVar()</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Converts the argument to an equivalent variant data type</FONT></TABLE><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Caution: </B>If the converted argument won't fit in the target data type, Visual Basic issues the Overflow error message shown in Figure 13.2. You've got to make sure, perhaps through an initial If statement, that the argument fits within the range of the converted data type.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>Normally, the following assignment stores .1428571 in the label named lblValue:<BR><BR><PRE><FONT COLOR="#000080">lblValue.Caption = (1 / 7) ' Assigns .1428571</FONT></PRE><P>The following, however, adds precision to the answer for a more accurate calculation:<BR><BR><PRE><FONT COLOR="#000080">lblValue.Caption = CDbl(1 / 7) ' Assigns .142857142857143</FONT></PRE><P><a href="13vel02.gif"><B>Figure 13.2. Don't overflow the target data type.</B></a><BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE>Don't even <I>mention</I> other bases!: If you <I>really</I> want to get fancy with numeric conversions, Visual Basic supports four functions that convert their decimal base-10 arguments to octal (base-8) and hexadecimal (base-16) return values. You'll want to use these functions if you write advanced applications that deal with other number bases.<BR>The two hexadecimal functions, Hex() and Hex$(), convert their numeric arguments to a variant and string hexadecimal value, respectively. The two octal functions, Oct() and Oct$(), convert their numeric arguments to a variant and string octal value, respectively.<BR>If you think that you won't use these functions, forget them! These functions, especially the hexadecimal functions, come in handy for programmers who write system-level applications such as memory- and disk-maintenance programs.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>The Asc() function converts its string argument to a corresponding ASCII table value. Generally, you'll pass a one-character string (or a variant that can equate to a string) value to Asc(). If the argument contains more than one character, Asc() ignores all characters following the first one.<BR><P>The following statement stores the number 65 in the numeric variable named Initial because 65 is the uppercase letter <I>A</I> in the ASCII table:<BR><BR><PRE><FONT COLOR="#000080">Initial = Asc("A")</FONT></PRE><P>Table 13.3 lists the remaining math functions that Visual Basic provides. Many of the functions are scientific and mathematical. You may not need any or all of the functions unless you write heavy, math-intensive applications.<BR><BR><P ALIGN=CENTER><CENTER><FONT COLOR="#000080"><B>Table 13.3. The scientific and mathematical functions.</B></FONT></CENTER><BR><TABLE BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="100%" CELLPADDING=2 ><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Function</I></FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Description</I></FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Abs()</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Returns the absolute value of the argument</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Atn()</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Returns the computed arc tangent of the argument expressed in radians</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Cos()</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Returns the computed cosine of the argument expressed in radians</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Exp()</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Returns the base of the natural logarithm argument</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Log()</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Returns the natural logarithm of the argument.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Sin()</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Returns the computed sine of the argument expressed in radians</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Sqr()</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Returns the square root of the argument</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Tan()</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Returns the computed tangent of the argument expressed in radians</FONT></TABLE><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Tip: </B>If you compute trigonometric values on arguments expressed as degrees instead of radians, multiply the argument by <I>pi</I> and divide by 180. The following expression assigns the sine of 38 degrees to a variable named sVal:</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE>sVal = Sin(38 * 3.14159 / 180)</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>The numeric functions will help you write programs when you need to include mathematical calculations in the code. Many business, graphics, and research applications sometimes utilize complex routines, and Visual Basic's math functions will help shorten your programming time and improve the accuracy of your code.<BR><BR><A NAME="E68E101"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>The String Functions</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>There are several Visual Basic string functions that manipulate string data better than most other programming languages allow. The string functions allow you to strip away characters from string data and change strings in various ways.<BR><P>The Visual Basic programming language supports strings using the variable-length and fixed-length strings that you've seen defined throughout the first half of this book. The string manipulation provided by Visual Basic is perhaps more powerful than any other non-BASIC programming language in widespread use today.<BR><P>You've already seen the Chr$() function that converts an ASCII number to its character equivalent. (There is a related Chr() function that returns a character in the variant data type also.) The Str$() (and the related variant-returning Str() function) converts a number to a string.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Tip: </B>Convert numbers to strings when you want to display numeric quantities inside a message box.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>Suppose that the user's age were stored in a numeric variable named Age. If you wanted to display the user's age in a message box, you couldn't do so without first converting the age to a string value as follows:<BR><BR><PRE><FONT COLOR="#000080">MsgBox "Your age is now" & Str$(Age)</FONT></PRE><P>The string case-conversion functions convert their string arguments to uppercase or lowercase character strings. Table 13.4 lists these functions.<BR><BR><P ALIGN=CENTER><CENTER><FONT COLOR="#000080"><B>Table 13.4. The case-conversion functions.</B></FONT></CENTER><BR><TABLE BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="100%" CELLPADDING=2 ><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Function</I></FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Description</I></FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>LCase()</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Returns the string argument as a variant data type that's all lowercase letters</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>LCase$()</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Returns the string argument as a string data type that's all lowercase letters</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>UCase()</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Returns the string argument as a variant data type that's all uppercase letters</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>UCase$()</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Returns the string argument as a string data type that's all uppercase letters</FONT></TABLE><P>If the argument already contains one or more characters in the target case, the functions leave those characters alone. The remarks following these statements indicate the function return values:<BR><PRE><FONT COLOR="#000080">lowerName = LCase("Larry") ' Returns larryupperName = UCase("Smythe") ' Returns SMYTHE</FONT></PRE><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><I>Definition: </I>A <I>substring</I> is a portion of a string.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>Visual Basic contains three string functions that return left, center, or right portions of a string. These are the Left$(), Mid$(), and Right$() functions. All three functions are said to return a substring value. Here are the formats of these functions:<BR><PRE><FONT COLOR="#000080">Left$(StringVal, length)Right$(StringVal, length)Mid$(StringVal, startVal, length)</FONT></PRE><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>The Mid$() function is called the <I>midstring</I> function.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>The Left$() function returns <I>length</I> characters from the <I>StringVal</I>. The <I>StringVal</I> might be a string constant or variable. Likewise, Right$() returns <I>length</I> characters from the <I>StringVal</I>. Mid$() returns <I>length</I> characters from the <I>StringVal</I> beginning at the <I>startVal</I> character.<BR><P>The remarks following these statements indicate the function return values:<BR><PRE><FONT COLOR="#000080">Title = "Something Good!"lTitle = Left$(Title, 4) ' SomerTitle = Right$(Title, 5) ' Good!mTitle = Mid$(Title, 5, 8) ' thing Go</FONT></PRE><P>Figure 13.3 shows how Visual Basic strips these substrings from the longer string values.<BR><P><B> <A HREF="13vel03.gif">Figure 13.3. Substring functions pull substrings from strings.</A></B><BR><P>The Len() function returns the number of characters stored in strings. Often, you'll have to adjust control sizes to hold long strings. You can use the Len() function as a guide to calculate how wide a control must be. The following statement stores 14 in the variable named Length:<BR><P>length = Len("Blue and Green")<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>The Len() function works for numeric values as well. If you pass a numeric variable, constant, or expression to Len(), Len() returns the amount of memory needed to hold that numeric value.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>The LTrim$() and RTrim$() functions trim extra spaces from the beginning or end of a string. If extra spaces don't exist, the LTrim$() and RTrim$() functions do nothing. LTrim$() returns the argument's string without any leading spaces. RTrim$() returns the argument's string without any trailing spaces. The Trim$() function handles both jobs by trimming both leading and trailing spaces from a string.<BR><P>Here are the formats of the string-trimming functions:<BR><PRE><FONT COLOR="#000080">LTrim[$](StringExpression)RTrim[$](StringExpression)Trim[$](StringExpression)</FONT></PRE><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE>There are also equivalent variant functions called LTrim(), RTrim(), and Trim().</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>The following statements trim spaces from the beginning, end, or both sides of strings:<BR><PRE><FONT COLOR="#000080">LStr1 = LTrim$(" Jonah") ' Stores JonahRStr2 = RTrim$("Jonah ") ' Stores JonahAnySt3 = Trim$(" Jonah ") ' Stores Jonah</FONT></PRE><P>Without the trimming functions, the spaces are copied into the target variables along with the name Jonah.<BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -