The basic operation of the Delphi string--Go

Source: Internet
Author: User
Tags control characters

Reprint: http://www.cnblogs.com/pchmonster/archive/2011/12/16/2290034.html

Learn together with this blog: http://www.cnblogs.com/xumenger/p/4427957.html

In practical programming, these operations are often used. At first you don't have to worry about remembering them all, just know that there are these features and you can find them when you need them.

1. Using the + operator to concatenate strings
123456789101112 var  X: Integer;  S1: string;  S2: string;begin  S1 := ‘Hello‘;  S2 := ‘World‘;  ShowMessage(S1 + ‘ ‘+ S2); { 连接了三个字符串S1,S2,和空格字符串}  X := 2011;  ShowMessage(‘今年是 ‘+ IntToStr(X)); { IntToStr函数将整型转换为字符串类型}end;

The message boxes are displayed in turn:

2. Foot Mark operator []

The PIN operator allows you to extract individual characters from a string.

12345678 var    s1: string    s2: string begin    s1: = ' Hello World '    s2: = s1[ 1 ;    showmessage (S2);  {will get the first character of the S1 string H} end

The string is starting at 1, the first character in the string is in s[1], and the 0 element (S[0]) of the short string (shortstring) contains the length of the string, not the first character of the string. Cannot access x[0 in long string (ansistring) and wide string (widestring).

3. Control characters in Strings

String Constants (string literal or string constant) are typically enclosed in two single quotes, and if you want the string itself to have single quotes, you need to use two single quotes, as follows:

12345 ' BORLAND '             {BORLAND} ' ' ll see '        {you'll see} " "                 {'}                    {null string} Code class= "pas string" "                   {a space}

You can embed control characters in a string, which is useful when you need to add some nonprinting characters to a string. Embedded control characters in the string are used to send to the serial device.

Use the character # to add control characters to a string, for example:

12345678 var  S1: string;  S2: string;begin  S1 := #89#111#117;  S2 := ‘You‘;       { 实际上S1和S2是一样}  ShowMessage(S1 + #13#10+ S2); { #13#10表示换行回车carriage-return-line-feed}end;

The following results are displayed after running:

4. Extending strings with multiple lines of code

A string enclosed in single quotation marks is called a string constant (string literal or string constant), which can have a maximum of 255 characters.

In order to enhance readability and maintainability, it is necessary to divide a string into multiple lines, and the + operator is required, for example:

1234 ShowMessage(‘This is very, very long message ‘+  ‘that seems to go on and on forever. In order ‘ +  ‘to make the code more readable the message has ‘+  ‘been split across several lines of code.‘);
5. Comparison of strings

String comparison operators include: = (equals), <> (unequal), < (less than), > (greater than), <= (less than or equal), >= (greater than or equal to).

These operators compare strings based on ASCII values. In most cases, you only need to use the equals sign to determine whether a string is equal to a fixed value or not equal to a fixed value. For example:

123456789 var  S1: string;begin  S1 := ‘Hello World‘;  ifS1 = ‘Hello World‘then    ShowMessage(‘相等‘)     { 将显示“相等”的消息框}  else    ShowMessage(‘不相等‘);end;

The results of the operation are as follows:

6. Function of string manipulation

The basic commonly used string manipulation functions are defined in the system or Sysutils unit, and we use an example to explain the usage of the basic commonly used string functions.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 6667 const  S1: ShortString= ‘Hello World‘;var  X: Integer;  SP: PChar;  S2: string;  RS: string; { 显示最后的结果}begin  SP := ‘Pchar‘;  //copy函数获取从最后一个字符开始,共一个字符  S2 := Copy(S1, Length(S1), 1);  { S2 = ‘d‘}  RS := RS + S2 + #13#10;  //删除字符串中指定位置字符的Delete函数  S2 := S1;  Delete(S2, 1, 1);               { S2 = ‘ello World‘}  RS := RS + S2 + #13#10;  //格式化字符串Format函数  S2 := Format(‘Time is : %d:%d:%d‘, [12, 15, 40]);  { S2 = ‘Time is : 12:15:40‘}  {%d表示将在这里放置一个整数,[2011]表示放置2011这个数值}  RS := RS + S2 + #13#10;  //数值转换为字符串InttoStr函数  X := 65535;  S2 := IntToStr(X); { S2 = ‘10‘}  RS := RS + S2 + #13#10;  //获取字符串长度Length函数  X := Ord(S1[0]);   { X = 11}  X := Length(S1);   { 因为S1为ShortString类型,所以可以通过S1[0]来获取字符长度}  RS := RS + IntToStr(X) + #13#10;  //转化为小写LowerCase函数  S2 := LowerCase(S1);  { S2 = ‘hello world‘}  RS := RS + S2 + #13#10;  //转化为大写UpperCase函数  S2 := UpperCase(S1);  { S2 = ‘HELLO WORLD‘}  RS := RS + S2 + #13#10;  //返回指定字符重复的字符串  S2 := StringOfChar(‘A‘, 10);  { S2 = ‘AAAAAAAAAA‘}  RS := RS + S2 + #13#10;  //StrPas把一个空结尾字符串(PChar或字符数组)转化为Pascal字符串  S2 := StrPas(SP);    { S2 = ‘Pchar‘}  RS := RS + S2 + #13#10;  //StrPCopy把一个Pascal字符串转化为空结尾字符串  StrPCopy(SP, ‘World Hello‘);   { SP = ‘World Hello‘}  RS := RS + StrPas(SP) + #13#10;  //Trim用于去除字符串前后的空格和控制字符  S2 := #13#10‘ Hello World   ‘;  S2 := Trim(S2);            { S2 = ‘Hello World‘}  RS := RS + S2 + #13#10;  //StringReplace用于替换字符串中制定字符  S2 := ‘AABBCC‘;  S2 := StringReplace(S2, ‘A‘, ‘E‘, [rfReplaceAll]);  RS := RS + S2 + #13#10;  ShowMessage(RS);end;

The results of the operation are as follows:

The above code is tested successfully in DELPHI7.

The basic operation of the Delphi string--Go

Related Article

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.