識用Visual Basic 2005 請進

2007-09-27 11:17 pm
請先看這個 : http://hk.geocities.com/jason10312008/Lab3.6.doc
括號內該打甚麼才能造到Lab3.6的效果?

Dim fullName, firstName, lastName As String
Dim n As Integer
fullName = txtName.Text
n = fullName.IndexOf(" ")
firstName = fullName.Substring( )
lastName = fullName.Substring( )
更新1:

With lstResults.Items .Clear() .Add("First name: " & firstName) .Add("Your last name has " & _ lastName.Length & " letters.") 剛才打打不哂,現補充。 感激不盡。

回答 (1)

2007-09-28 9:16 am
✔ 最佳答案
Substring has two overload functions:
Substring(startIndex, length)
Substring(startIndex)

firstName = fullName.Substring(0, n).Trim()
lastName = fullName.Substring(n + 1).Trim()

n is the index of the space, lastName begins at one position after n.
The Trim() function removes any white space at both ends of the string. If the are two spaces between first and last name, the extra space will be removed.

One last thing, do not use string concatenation, try the following instead:
.Add(String.Format("Your last name has {0} letters", lastName.Length))


收錄日期: 2021-04-26 13:13:30
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20070927000051KK01492

檢視 Wayback Machine 備份