分享
 
 
 

SPLIT

王朝百科·作者佚名  2009-12-15
窄屏简体版  字體: |||超大  

Split 函数详解

●用法:传回数组 = Split(原始字串, 要找的字串, 拆成几个数组)

描述

返回一个下标从零开始的一维数组,它包含指定数目的子字符串。

语法

Split(expression[, delimiter[, count[, compare]]])

Split函数语法有如下几部分:

部分 描述

expression 必需的。包含子字符串和分隔符的字符串表达式 。如果expression是一个长度为零的字符串(""),Split则返回一个空数组,即没有元素和数据的数组。

delimiter 可选的。用于标识子字符串边界的字符串字符。如果忽略,则使用空格字符(" ")作为分隔符。如果delimiter是一个长度为零的字符串,则返回的数组仅包含一个元素,即完整的 expression字符串。

count 可选的。要返回的子字符串数,-1表示返回所有的子字符串。

compare 可选的。数字值,表示判别子字符串时使用的比较方式。关于其值,请参阅“设置值”部分。

语法

Visual Basic(声明)

Public Function Split ( _ ParamArray separator As Char() _) As String()

Visual Basic (用法)

Dim instance As StringDim separator As Char()Dim returnValue As String()returnValue = instance.Split(separator)

C#

public string[] Split( params char[] separator)

Visual C++

public:array<String^>^ Split( ... array<wchar_t>^ separator)

J#

public String[] Split( char[] separator)

JScript

public function Split( ... separator : char[]) : String[]

输出结果:

Visual Basic

Public Class SplitTest

Public Shared Sub Main() Dim words As String = "This is a list of words, with: a bit of punctuation."

Dim split As String() = words.Split(New [Char]() {" "c, ","c, "."c, ":"c})

For Each s As String In split

If s.Trim() <> "" Then

Console.WriteLine(s)

End If

Next s

End Sub 'Main

End Class 'SplitTest

' The example displays the following output to the console:

' This

' is

' a

' list

' of

' words

' with

' a

' bit

' of

' punctuation

C#

using System;public class SplitTest { public static void Main() { string words = "This is a list of words, with: a bit of punctuation."; string [] split = words.Split(new Char [] {' ', ',', '.', ':'}); foreach (string s in split) { if (s.Trim() != "") Console.WriteLine(s); } }}// The example displays the following output to the console:// This// is// a// list// of// words// with// a// bit// of// punctuation

Visual C++

using namespace System;using namespace System::Collections;int main(){ String^ words = "This is a list of words, with: a bit of punctuation."; array<Char>^chars = {' ',',','->',':'}; array<String^>^split = words->Split( chars ); IEnumerator^ myEnum = split->GetEnumerator(); while ( myEnum->MoveNext() ) { String^ s = safe_cast<String^>(myEnum->Current); if ( !s->Trim()->Equals( "" ) ) Console::WriteLine( s ); }}// The example displays the following output to the console:// This// is// a// list// of// words// with// a// bit// of// punctuation

J#

import System.*;public class SplitTest{ public static void main(String[] args) { String words = "This is a list of words, with: a bit of punctuation."; String split[] = words.Split(new char[] { ' ', ',', '.', ':' }); for (int iCtr = 0; iCtr < split.get_Length(); iCtr++) { String s = (String)split.get_Item(iCtr); if (!(s.Trim().Equals(""))) { Console.WriteLine(s); } } } //main} // The example displays the following output to the console:// This// is// a// list// of// words// with// a// bit// of// punctuation

JScript

import System;public class SplitTest { public static function Main() : void { var words : String = "This is a list of words, with: a bit of punctuation."; var separators : char[] = [' ', ',', '.', ':']; var split : String [] = words.Split(separators); for (var i : int in split) { var s : String = split[i]; if (s.Trim() != "") Console.WriteLine(s); } }}SplitTest.Main();// The example displays the following output to the console:// This// is// a// list// of// words// with// a// bit// of// punctuation

设置值

compare参数的设置值如下:

常数 值 描述

vbUseCompareOption -1 用Option Compare语句中的设置值执行比较。

vbBinaryCompare 0 执行二进制比较。

vbTextCompare 1 执行文字比较。

vbDatabaseCompare 2 仅用于Microsoft Access。基于您的数据库的信息执行比较。

Private Sub Command1_Click()

Dim MyStr As String

MyStr = "1234567123456712345"

MyStrs = Split(MyStr, "67")

For Each Strs In MyStrs

Print Strs

Next

End Sub

●输出结果:"12345"、"12345"、"12345"

======================================================================================

'这个VB程序是让求10个学生的考试成绩的平均分..

'比如95 85 70 75 80 90 60 65 95 100

'这10个人的分数的平均分...

Private Sub Form_Load()

Dim A$(), i As Long, intB As String, s As Integer

If Dir("d:平均分.dat") = vbNullString Then

Open "d:平均分.dat" For Output As #1

Print #1, "95 85 70 75 80 90 60 65 95 100"

Close #1

End If

Open "d:平均分.dat" For Input As #1

Input #1, intB

Close #1

A = Split(intB, Space(1), -1, 1)

For i = 0 To UBound(A, 1)

Debug.Print A(i); " ";

s = s + A(i)

Next i

Debug.Print ",10个学生的平均成绩是 :" & s / 10

End Sub

======================================================

Private Sub command1_Click()

Dim AString As String

Dim r() As String '把变量按照“,”分割出来的数组

Dim rt As String '最终的结果,用换行符代替“,”

Dim C As Integer '这个是循环用的

AString = "高级,中级,低级,先进"

r = Split(AString, ",") '把每个目录都分解出来

For C = 0 To UBound(r) 'C由0开始循环到r数组的最大下标

rt = rt & vbCrLf & vbCrLf & r(C) '把数组的每一个元素都添加到rt,用回车分割

Next C '循环

MsgBox rt '输出

End Sub

==============================================================

Private Sub Form_Load()

Dim strTextDate As String

strTextDate = "2008-12-1 星期一"

MsgBox Format(Split(strTextDate)(0), "yyyy-mm-dd")

End Sub

当小括号中写0时,返回数组中第一个元素,小括号中写1时返回数组中第二个元素。依此类推,用这种写法返回数据时,必须用一个空格把字符串分开,其它字符仅当做一个数据。例:

Private Sub Form_Load()

Dim AString As String

AString = "高级 中级 低级 先进"

MsgBox Split(AString)(0)

MsgBox Split(AString)(1)

MsgBox Split(AString)(2)

MsgBox Split(AString)(3)

End Sub

=======================================

以下只返回 高级,中级,低级,先进 仅当作一个串,即只能返回Split(AString)(0)的值,其它值都产生下标越界错误。所以用以下方法分解时,只能用一个空格分割,而不能用其它字符分割.

Private Sub Form_Load()

Dim AString As String

AString = "高级,中级,低级,先进"

MsgBox Split(AString)(0)

MsgBox Split(AString)(1)

MsgBox Split(AString)(2)

MsgBox Split(AString)(3)

End Sub

split 命令用途

将文件分割成几段。

语法

要将一个文件分割成包含指定行数的多个文件

split [ -l LineCount ] [ -a SuffixLength ] [ File [ Prefix ] ]

要将一个文件分割成包含指定字节数的多个文件

split -b Number [ k | m ] [ -a SuffixLength ] [ File [ Prefix ] ]

描述

split 命令读取指定文件,以 1000 行大小写在一组输出文件上。第一个输出文件名由指定前缀(缺省值 x)和 aa 后缀组合构成,第二个文件名由前缀和 ab 后缀组合构成,如此按字典顺<img class="img InsertH2" alt="段落标题" src="../../System/_resource/blank.gif">序一直到 zz(最多 676 个文件)。后缀的字母数及因此的输出名称文件数可用 -a 标志增加。

您指定的 Prefix 不能长于 PATH_MAX - 2 个字节(如果指定了 -a 标志,则不能长于 PATH_MAX - SuffixLength 个字节)。PATH_MAX 变量指定系统的最大路径名的长度( 在 /usr/include/sys/limits.h 文件中定义)。

如果您不指定输入文件或如果您指定 -(减号)文件名,那么 split 命令从标准输入读取文件。

标志

注:-b 和 -l 标志是互斥的。

-a SuffixLength 指定用于形成输出名称文件后缀部分的字母数。字母数确定可能的输出文件名组合数。缺省是两个字母。

-b Number 将文件分割成 Number 变量指定的字节数。将 k(千字节)或 m(兆字节)乘数加到 Number 值的末尾使文件分别分割成 Number*1024 字节或 Number*1,048,576 字节的几个段。

-l LineCount 指定每个输出文件的行数。缺省值是 1000 行。

退出状态

该命令返回以下退出值:

0 命令成功运行。

>0 发生错误。

示例

1. 要将文件分割成 1000 行的段,请输入:

split book

此示例将 book 分割成 1000 行的段,命名为 xaa、 xab、 xac 等等。

2. 要将文件分割成 50 行的段并指定文件名前缀,请输入:

split -l 50 book sect

此示例将 book 分割成 50 行的段,命名为 sectaa、sectab、sectac 等等。

3. 要将文件分割成 2KB 的段,请输入:

split -b 2k book

此示例将 book 分割成 2*1024 字节的段,命名为 xaa、xab、xac 等等。

4. 要将文件分割成 676 个以上的段,请输入:

split -l 5 -a 3 book sect

此例将 book 分割成 5 行的段,命名为 sectaaa、sectaab、 sectaac 等等,直到 sectzzz(最多 17,576 个文件)。

文件

/usr/bin/split 包含 split 命令。

java里面

split

publicString[] split(String regex)Splits this string around matches of the given regular expression. This method works as if by invoking the two-argumentsplitmethod with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array. The string "boo:and:foo", for example, yields the following results with these expressions:Regex

Result

:

{ "boo", "and", "foo" }

o

{ "b", "", ":and:f" }

Parameters: regex - the delimiting regular expression Returns: the array of strings computed by splitting this string around matches of the given regular expression Throws: PatternSyntaxException - if the regular expression's syntax is invalid Since: 1.4 See Also: Pattern

split

public String[]split(String regex, int limit)根据匹配给定的正则表达式来拆分此字符串。 此方法返回的数组包含此字符串的每个子字符串,这些子字符串由另一个匹配给定的表达式的子字符串终止或由字符串结束来终止。数组中的子字符串按它们在此字符串中的顺序排列。如果表达式不匹配输入的任何部分,则结果数组只具有一个元素,即此字符串。 limit 参数控制模式应用的次数,因此影响结果数组的长度。如果该限制n大于 0,则模式将被最多应用n- 1 次,数组的长度将不会大于n,而且数组的最后项将包含超出最后匹配的定界符的所有输入。如果n为非正,则模式将被应用尽可能多的次数,而且数组可以是任意长度。如果n为零,则模式将被应用尽可能多的次数,数组可有任何长度,并且结尾空字符串将被丢弃。 例如,字符串 "boo:and:foo" 使用这些参数可生成下列结果:Regex

Limit

结果

:

2

{ "boo", "and:foo" }

:

5

{ "boo", "and", "foo" }

:

-2

{ "boo", "and", "foo" }

o

5

{ "b", "", ":and:f", "", "" }

o

-2

{ "b", "", ":and:f", "", "" }

o

0

{ "b", "", ":and:f" }

这种形式的方法调用str.split(regex,n) 产生与以下表达式完全相同的结果: Pattern.compile(regex).split(str,n)

参数:regex - 定界正则表达式 limit - 结果阈值,如上所述返回:字符串数组,根据给定正则表达式的匹配来拆分此字符串,从而生成此数组抛出:PatternSyntaxException - 如果正则表达式的语法无效(JavaAPI)

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有