共用方式為


Interaction.Partition 方法

傳回字串,表示包含數字的計算範圍。

命名空間: Microsoft.VisualBasic
組件: Microsoft.VisualBasic (在 microsoft.visualbasic.dll 中)

語法

'宣告
Public Shared Function Partition ( _
    Number As Long, _
    Start As Long, _
    Stop As Long, _
    Interval As Long _
) As String
'用途
Dim Number As Long
Dim Start As Long
Dim Stop As Long
Dim Interval As Long
Dim returnValue As String

returnValue = Interaction.Partition(Number, Start, Stop, Interval)
public static string Partition (
    long Number,
    long Start,
    long Stop,
    long Interval
)
public:
static String^ Partition (
    long long Number, 
    long long Start, 
    long long Stop, 
    long long Interval
)
public static String Partition (
    long Number, 
    long Start, 
    long Stop, 
    long Interval
)
public static function Partition (
    Number : long, 
    Start : long, 
    Stop : long, 
    Interval : long
) : String

參數

  • Number
    必要項。Long - 在其中一個計算範圍中所要尋找的整數值。
  • Start
    必要項。Long。整數,表示這組計算範圍的開頭。Start 不可小於 0。
  • Stop
    必要項。Long。整數,表示這組計算範圍的結尾。Stop 不可小於或等於 Start。
  • Interval
    必要項。Long。整數,表示 Start 與 Stop 間所計算之每個範圍的大小。Interval 不可小於 1。

傳回值

傳回字串,表示包含數字的計算範圍。

備註

如需詳細資訊,請參閱 Visual Basic 的主題 Partition 函式

Partition 函式會計算一組數字範圍,每一個範圍都包含 Interval 指定之值的數目。第一個範圍開始於 Start,而最後一個範圍則結束於 Stop。然後 Partition 函式可識別哪一個範圍包含 Number,並傳回描述該範圍的字串。此範圍會在字串中表示為 "lowervalue:uppervalue",其中範圍的低端 (lowervalue) 與高端 (uppervalue) 會以冒號 (:) 做為區隔。

必要時,Partition 函式會在 lowervalueuppervalue 之前插入前置空格,使得這兩者都會與此值的字串表示 (Stop + 1) 有相同的字元數。這可確保在您將 Partition 函式的輸出與 Number 的數個值一起使用時,會在任何後續的排序作業期間適當地處理產生的文字。

下表將顯示使用三組 Start、Stop 和 Interval 所計算範圍的一些字串範例。「第一範圍」和「最後範圍」欄會顯示在已知 Start 和 Stop 值的狀況下,最低和最高的可能範圍。「第一範圍之前」和「最後範圍之後」欄會分別顯示小於 Start 和大於 Stop 之 Number 值所傳回的字串。

Start

Stop

Interval

第一範圍之前

第一範圍

最後範圍

最後範圍之後

0

99

5

"   : -1"

"  0:  4"

" 95: 99"

"100:   "

20

199

10

"   : 19"

" 20: 29"

"190:199"

"200:   "

100

1010

20

"    : 99"

" 100: 119"

"1000:1010"

"1011:    "

在上表中,第三行會顯示當 Start 和 Stop 所定義的一組數字不能由 Interval 均分時,所產生的結果。最後範圍結束於 Stop,所以它只有 11 個數字的長度,但是 Interval 卻是 20。

如果 Interval 為 1,則此範圍會是 "Number:Number",不論 Start 和 Stop 引數為何。例如,如果 Number 為 267、Stop 為 1000,且 Interval 為 1,則 Partition 會傳回 "267: 267"。

在建構資料庫查詢時,Partition 可能會很實用。您可以建立一個 SELECT 查詢,查詢中顯示各種數值範圍內發生的訂單數目,例如,從發票值 1 到 1000、1001 到 2000 等範圍。

範例

下列範例會設定從 1950 年代到 2049 年代的一系列範圍,範例中會在適當的範圍內尋找 year 的值,並傳回顯示該範圍的 String 值。例如,如果 year 的值為 1984,則 Partition 會傳回 "1980:1989"。

Dim year As Long = 1984
' Assume the value of year is provided by data or by user input.
Dim decade As String
decade = Partition(year, 1950, 2049, 10)
MsgBox("Year " & CStr(year) & " is in decade " & decade & ".")

平台

Windows 98、 Windows 2000 SP4、 Windows CE、 Windows Millennium Edition、 Windows Mobile for Pocket PC、 Windows Mobile for Smartphone、 Windows Server 2003、 Windows XP Media Center Edition、 Windows XP Professional x64 Edition、 Windows XP SP2、 Windows XP Starter Edition

.NET Framework 並不支援各種平台的所有版本。如需支援平台版本的相關資訊,請參閱系統需求一節的內容。

版本資訊

.NET Framework

支援版本:2.0、1.1、1.0

.NET Compact Framework

支援版本:2.0、1.0

請參閱

參考

Interaction 類別
Interaction 成員
Microsoft.VisualBasic 命名空間
Structured Query Language (SQL)

其他資源

Partition 函式