📄 faq46.htm
字号:
<HTML>
<HEAD>
<TITLE>Convert an AnsiString to all lower case or upper case</TITLE>
<META NAME="Author" CONTENT="Harold Howe">
</HEAD>
<BODY BGCOLOR="WHITE">
<CENTER>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH="640">
<TR>
<TD>
<H3>
Convert an <TT>AnsiString</TT> to all lower case or upper case
</H3>
<P>
The <TT>AnsiString</TT> class provides two member functions called <TT>UpperCase</TT> and
<TT>LowerCase</TT> that will accomplish what you need. <TT>UpperCase</TT> and
<TT>LowerCase</TT> are prototyped in the file <TT>DSTRING.H</TT>. The prototypes look like
this:
</P>
<pre>
AnsiString <b>__fastcall</b> LowerCase<b>(</b><b>)</b> <b>const</b><b>;</b>
AnsiString <b>__fastcall</b> UpperCase<b>(</b><b>)</b> <b>const</b><b>;</b>
</pre>
<P>
Notice that the functions are declared as <TT>const</TT> methods of <TT>AnsiString</TT>.
This allows you to call <TT>UpperCase</TT> or <TT>LowerCase</TT> on a constant
<TT>AnsiString</TT> object. The <TT>const</TT> declaration means that <TT>UpperCase</TT>
and <TT>LowerCase</TT> won't modify the current <TT>AnsiString</TT> object. This has an
important consequence: <TT>LowerCase</TT> and <TT>UpperCase</TT> return a new
<TT>AnsiString</TT> object that contains the converted string from the source object.
The original <TT>AnsiString</TT> object is not changed. Here is a code example that shows
how to use the functions:
</P>
<pre>
<font color="navy">// Example of how to correctly use UpperCase and LowerCase</font>
AnsiString str <b>=</b> <font color="blue">"Hello Dr. Crane"</font><b>;</b>
AnsiString upper <b>=</b> str<b>.</b>UpperCase<b>(</b><b>)</b><b>;</b> <font color="navy">// result= HELLO DR. CRANE</font>
AnsiString lower <b>=</b> str<b>.</b>LowerCase<b>(</b><b>)</b><b>;</b> <font color="navy">// result= hello dr. crane</font>
<font color="navy">// Example of code that does nothing</font>
AnsiString str <b>=</b> <font color="blue">"Hello Dr. Crane"</font><b>;</b>
str<b>.</b>UpperCase<b>(</b><b>)</b><b>;</b> <font color="navy">// code has no effect, str is not modifed by UpperCase.</font>
</pre>
<P>
<B>Note:</B> The <TT>UpperCase</TT> and <TT>LowerCase</TT> methods of <TT>AnsiString</TT>
work by calling two global functions called <TT>AnsiUpperCase</TT> and <TT>AnsiLowerCase</TT>
in <TT>SYSUTILS.PAS</TT>. You can use the global functions if you prefer.
</P>
<P>
<B>Note:</B> I once needed to test a command line parameter as part of a programming
assignment. I used the code below to provide a case insensitive way of testing the
command line item.
</P>
<pre>
<b>if</b><b>(</b> <b>(</b>ParamCount<b>(</b><b>)</b> <b>>=</b> <font color="blue">1</font><b>)</b> <b>&&</b> <b>(</b>ParamStr<b>(</b><font color="blue">1</font><b>)</b><b>.</b>UpperCase<b>(</b><b>)</b> <b>==</b> <font color="blue">"-ISX"</font><b>)</b><b>)</b>
<b>{</b>
<font color="navy">// do some cool stuff.</font>
<b>}</b>
</pre>
</TD> </TR>
</TABLE>
</CENTER>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -