ICT
the blog for ICT
ken013194
暱稱: ホロ
性別: 男
國家: 香港
地區: 葵青區
« July 2026 »
SMTWTFS
1234
567891011
12131415161718
19202122232425
262728293031
最新文章
Text File
Arrays
String Procedures
String Functions
String Operation
文章分類
全部 (23)
Programming Language (23)
訪客留言
最近三個月尚無任何留言
每月文章
日誌訂閱
尚未訂閱任何日誌
好友名單
尚無任何好友
網站連結
尚無任何連結
最近訪客
最近沒有訪客
日誌統計
文章總數: 23
留言總數: 0
今日人氣: 0
累積人氣: 87
站內搜尋
RSS 訂閱
RSS Feed
2009 年 9 月 11 日  星期五   晴天


String Operation 分類: Programming Language

High-level Programming Language (Pascal)

Basic Operations on Strings

Assignment

  • for example,

     Name := 'Chan Tai Man';
     Class := 'F4A';
     Address := 'Rm 5, 20 / F, 108 School Road, Hong Kong.';

    • the value being assigned may be a string shorter than the declared length of the variable, the lengths of Name and Address are 12 and 40 characters respectively
    • if the length of the value being assigned is longer than the declared length of the variable, the value is truncated to the size of the variable, the actual value of Class is 'F4'

     

Input/Output

  • Inuput
    • use the readln statement to read a value from the keyboard for a string variable
    • input string value does not need to be enclosed in quotation marks
    • if the input string contains more characters than the declared length, any excess characters are ignored
    • for example, readln (Name);
    • use two readln statements to read and assign two values to two different string variables,

      for example,

       readln (Name);
       readln (Class);

  • Output
    • using the write or writeln statements to output the string constant or the value of a string variable
    • for example, writeln ('My name is ', Name);

     

    Accessing individual characters of a string

  • for example,
    • Name[2] is used to access the second character of the string variable Name
    • the following is used to output the characters of a string one by one

       for i := 1 to 5 do
        writeln (Name[i]);
       
  • string variable of length 1 and a character variable are not compatible (i.e. a string variable of length 1 cannot be assigned to a character variable)


    Comparison

  • strings can be compared with the following relational operators : <, >, =, <=, >= and <>
  • a character is said to be greater than another if its ASCII value is larger,

    for example,

     'G' is greater than 'A'

     since the ASCII value of the letter 'G' is 71 and that of 'A' is 65

  • two strings are compared, character by character, using their ASCII values

    for example,

     'GRAPES' > 'APPLE' is true because 'G' follows 'A'
     'Computers' < 'Computing' is true because 'e' precedes 'i'

  • uppercase letters have smaller ASCII values than lowercase letters

for example,

'DAVID' is less than 'David', because 'A' is less than 'a'

  • leading and trailing blank spaces are significant in string comparisons

    for example,

    'computer' is less than 'computer' because the blank space comes before the letter 'c' in the ASCII table
    'computer' is greater than the string 'computer' because the former one is longer

Initialization of string variable

  • uninitialized string may contain unpredictable value
  • Name := '';
    {initialize a string variable Name with a null string, there is no space between the quotation marks}
  • Null string (empty string) is a string with zero character
    Name := ' '; {initialize a string variable Name with one space}

 






訪客留言 (返回 ken013194 的日誌)

訪客名稱:
電郵地址: (不會公開)
驗證碼:  按此更新驗證碼 (如看不清楚驗證碼請點擊圖片刷新)
俏俏話: (必需 登入 後才能使用此功能)
[ 開啟多功能編輯器 ]