A-level 計算機/AQA/試卷 1/程式設計基礎/字串處理
非常流行的考試問題涉及字串操作。這些簡單的函式將幫助您完成此任務。
此函式用於查詢傳遞給它的任何字串的長度,包括所有字元,包括空格。在 Visual Basic 中,要查詢字串的長度,我們使用 Len("some string") 函式,該函式返回傳遞給它的字串的整數長度
someText = "Gary had a little lamb"
Console.writeline(Len(someText))
此函式允許我們查詢給定字串中專案的的位置,並返回該位置的位置。在 Visual Basic 中,這是透過以下命令執行的:InStr([string], [item]) 例如,我們可能想透過查詢句號來查詢句子的結尾位置
someText = "Gary had a little lamb. His fleece was white as snow."
Console.writeline(InStr(someText,"."))
我們也可以使用此命令在字串中搜索字串。例如,如果我們要查詢句子是否包含某個名稱
someText = "Gary had a little lamb. Dave's fleece was white as snow."
Console.writeline(InStr(someText,"Dave"))
如果搜尋項不包含在字串中,它將返回 0
someText = "Gary had a little lamb. Dave's fleece was white as snow."
Console.writeline(InStr(someText,"Julie"))
此函式允許您從字串中剪下專案並返回子字串。Visual Basic 使用以下命令:[string].Substring([startPosition],[lengthOfReturnString])。例如,我們可能想從給定的固定電話號碼中查詢本地號碼。我們必須忽略區號
phone = "(01234)567890"
local = phone.Substring(7, 6)
console.writeline(local)
此函式允許您將字串貼上在一起(連線),以便您可以開始使用變數構建字串。Visual Basic 使用以下命令:[stringA & stringB] 例如,我們可能在變數 dim name as string 中儲存了使用者的姓名,以及我們想給予他們的問候
name = "Charles"
console.writeline("Hello " & name & ". How are you today?")
當您宣告變數時,您會為它提供一個數據型別。此資料型別限制了您可以放入變數的值。例如
dim age as integer- 將允許:
age = 34 - 將不允許:
age = "cabbages"
這似乎很有意義,但當您嘗試將實數放入整數中時會發生什麼
dim age as integer
age = 34.3
console.writeline(age)
這似乎沒問題,但在其他語言中我們可能會遇到麻煩。要執行此操作,我們必須從一種資料型別轉換為另一種資料型別
dim age as decimal
age = 34.3
console.writeline(age)
age = CInt(34.3) 'converts the decimal into an integer
console.writeline(age)
|
練習:字串函式 答案 Dim name As String
console.write("Input: ")
name = console.readline()
console.writeline("Hello " & name & " you have " & Len(name) & " letters in your name.")
有些人愚蠢地將他們的名字和姓氏輸入了資料庫,寫一些程式碼來顯示名字,然後是他們的姓氏 dim fistname as string = "Elizabeth Sheerin"
答案 Dim name As String = "Elizabeth Sheerin"
Dim firstname, secondname As String
Dim space, textlength As Integer
space = InStr(name, " ")
textlength = Len(name)
firstname = name.Substring(0, space)
secondname = name.Substring(space, textlength - space)
Console.WriteLine("first name is: " & firstname)
Console.WriteLine("second name is: " & secondname)
電話號碼已作為字串輸入計算機:(01234)567890 dim phonenum as string = "(01234)567890"
編寫一些程式碼以輸出沒有括號的號碼 答案 Dim phonenum As String = "(01234)567890"
Dim firstbracket, secondbracket As String
Dim textlength, arealength As Integer
firstbracket = InStr(phonenum, "(")
secondbracket = InStr(phonenum, ")")
textlength = Len(phonenum)
arealength = secondbracket - firstbracket
Console.Write(phonenum.Substring(firstbracket, arealength - 1) & phonenum.Substring(secondbracket, textlength - secondbracket))
與上面類似的問題,電話號碼目前以非常難以理解的格式儲存:01234567890,完全缺少區號。您可以將它們轉換為顯示前 5 位數字為區號嗎? dim phonenum as string = "01234567890"
然後應將其輸出為 答案 Dim phonenum As String = "01234567890"
Console.Write("(" & phonenum.Substring(0, 5) & ")" & phonenum.Substring(6, 5))
Console.ReadLine()
一個迴文是一個詞、短語或數字,無論從哪個方向讀都是一樣的。例如 1234321、RACECAR、TOOT 和 NUN。您需要編寫一個程式來檢查給定的任何輸入是否為迴文,並讓使用者知道 答案 Dim name As String
Dim length As Integer
Dim Pal As Boolean = TRUE
console.write("Input: ")
name = console.readline()
length = Len(name)
For x = 0 to (length / 2)
If name.Substring(x, 1) != name.Substring(length - x, 1) then
Pal = FALSE
End If
Next
If Pal then
console.writeline("That is a palindrome!")
Else
console.writeline("That is NOT a palindrome!")
End If
|
|
擴充套件:正則表示式 您通常希望檢查輸入字串的格式,如果格式不正確,您希望它重新提交。例如,您可能希望某人輸入他們最好的朋友的名字,這意味著他們不應該輸入任何字母或空格,並且它應該以大寫字母開頭 為此,我們可以將輸入字串與一些規則匹配,正則表示式或正則表示式,在這種情況下,我們只希望字母表中的字元
拆分規則
另一個例子可能是檢查著名作曲家的拼寫是否正確 "Handel", "Händel", and "Haendel" 我們可以使用模式
大多數正則表示式工具提供以下操作來構建表示式。 布林“或” 垂直線分隔備選方案。例如, 分組 括號用於定義運算子的範圍和優先順序(以及其他用途)。例如, 量化 在標記(如字元)或組後面的量詞指定前面元素允許出現的次數。
大多數程式語言都有正則表示式函式。在 VB.NET 中,我們可以使用 Regex 例程使用正則表示式 ' this code enforces the name rule from earlier
Dim name As String
Console.Write("Name of best friend: ")
name = Console.Readline()
' match the string against a regular expression
Dim m As Match = Regex.Match(name, "[A-Z][a-z]+")
If (m.Success) Then
Console.WriteLine("You have input the name correctly")
Else
Console.WriteLine("Incorrect format!")
End If
正則表示式的常見用途是檢查您是否輸入了正確的電子郵件地址。該規則如下: 您可以在維基百科上找到有關正則表示式的更多資訊,您將在 A2 中更詳細地學習正則表示式。 |
從整數、實數、日期/時間轉換。
