JAVA和javascrito_JAVA 和JavaScript的split方法異同

Split的方法很常用,除了str.split("regex"),其實還可以多傳一個參數:str.split("regex", limit)。但是要注意,JavaScript和java的split中limit參數作用是不同的。

簡單說,JavaScript中,limit是指分割后返回數組的元素個數,注意,返回值仍為數組。

Java中,limit是指regex參數用于匹配string的次數,尤其要注意,如果傳N,則將正則匹配N-1次。(不知道為什么,筆者就因為這個編碼出了bug。。)

JAVA

Splits this string around matches of the given regular expression.

This method works as if by invoking the two-argument split method 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" }

參數:

regex the delimiting regular expression

返回:

the array of strings computed by splitting this string around matches of the given regular expression

拋出:

PatternSyntaxException - if the regular expression's syntax is invalid

自:

1.4

另請參閱:

java.util.regex.Pattern

@spec

JSR-51

String[] java.lang.String.split(String regex, int limit)

Splits this string around matches of the given regular expression.

The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression

or is terminated by the end of the string. The substrings in the array are in the order in which they occur in this string.

If the expression does not match any part of the input then the resulting array has just one element, namely this string.

The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array.

If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n,

and the array's last entry will contain all input beyond the last matched delimiter. If n is non-positive then the pattern will be applied

as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible,

the array can have any length, and trailing empty strings will be discarded.

The string "boo:and:foo", for example, yields the following results with these parameters:

Regex Limit Result

: 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" }

An invocation of this method of the form str.split(regex, n) yields the same result as the expression

java.util.regex.Pattern.compile(regex).split(str, n)

參數:

regex the delimiting regular expression

limit the result threshold, as described above

返回:

the array of strings computed by splitting this string around matches of the given regular expression

拋出:

PatternSyntaxException - if the regular expression's syntax is invalid

自:

1.4

另請參閱:

java.util.regex.Pattern

@spec

JSR-51

JavaScript

split 方法

將一個字符串分割為子字符串,然后將結果作為字符串數組返回。

stringObj.split([separator[,?limit]])

參數

stringObj

必選項。要被分解的?String?對象或文字。該對象不會被?split?方法修改。

separator

可選項。字符串或?正則表達式?對象,它標識了分隔字符串時使用的是一個還是多個字符。如果忽略該選項,返回包含整個字符串的單一元素數組。

limit

可選項。該值用來限制返回數組中的元素個數。

說明

split?方法的結果是一個字符串數組,在?stingObj?中每個出現?separator?的位置都要進行分解。separator?不作為任何數組元素的部分返回。

示例

下面的示例演示了?split?方法的用法。

function SplitDemo(){

var s, ss;

var s = "The rain in Spain falls mainly in the plain.";???//在每個空格字符處進行分解。

ss = s.split(" ");

return(ss);

}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/378709.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/378709.shtml
英文地址,請注明出處:http://en.pswp.cn/news/378709.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

如果__name__ =='__main__':在Python中怎么辦?

In order to understand the details of __name__ variable and the if condition, let us go through a simple exercise. Run a simple python file with just the following lines and run the file as python3 code, 為了了解__name__變量和if條件的詳細信息,讓…

Eclipse C/C++開發環境搭建

1 Eclipse的安裝 到http://java.sun.com/j2se/1.5.0/download.jsp 下載JRE安裝; 到http://eclipse.org下載Eclipse安裝。(這兒可以下載Java版本的,也可以下載C/C 版本的) 2 對于下載的Java版本或著只下載Eclipse IDE的&#xff0c…

微機原理——尋址方式總結

一、操作數的尋址方式 立即尋址方式 格式: 操作碼 數字表達式(將數據送入寄存器中) 源操作數可以是8位也可以是16位。 MOV AH, F5H (字節操作) F5H稱為立即數(8位操作數) MOV AL, 8AH (字節操作) 8AH稱為…

15-輪廓檢測

邊緣是零零散散的,而輪廓是一個整體 cv2.findContours(img,mode,method) img:輸入圖像對象名稱 mode:輪廓檢索模式 RETR_EXTERNAL:只檢索最外面的輪廓 RETR_LIST:檢索所有的輪廓,并將其保存到一條鏈表當中…

拋硬幣仿真實驗java_探索HyperLogLog算法(含Java實現)

引言HyperLogLog算法經常在數據庫中被用來統計某一字段的Distinct Value(下文簡稱DV),比如Redis的HyperLogLog結構,出于好奇探索了一下這個算法的原理,無奈中文資料很少,只能直接去閱讀論文以及一些英文資料,總結成此文…

kotlin鍵值對數組_Kotlin程序以升序對數組進行排序

kotlin鍵值對數組Given an array, we have to sort its elements in ascending order. 給定一個數組,我們必須按升序對其元素進行排序。 Example: 例: Input:arr [10, 20, 5, 2, 30]Output:sorted array (Ascending Order): [2, 5, 10, 20, 30]在Kotl…

微機原理——總線和時序

前提 8088有兩個組態: 最大組態和最小組態,通過引腳MN/MX*的電平決定組態。(*表示低電平有效) 兩種組態沒有本質區別。 8088的引腳: 引腳可分為下面幾種類別: 1、數據和地址引腳 2、讀寫控制引腳 3、中斷…

PHP站內搜索:多關鍵字查找,加亮顯示

1、SQL語句中的模糊查找LIKE條件一般用在指定搜索某字段的時候, 通過"% _" 通配符的作用實現模糊查找功能,通配符可以在前面也可以在后面或前后都有。搜索以PHP100開頭: SELECT * FROM teble WHERE title LIKE PHP100% 搜索以PHP100結束&…

16-模板匹配

cv2.matchTemplate(img,template,cv2.TM_SQDIFF) 參數一:原圖圖像對象名稱 參數二:模板圖像對象名稱 參數三:差別程度的計算方法(六選一推薦使用帶歸一化的) 模板匹配和卷積原理很像,模板從原圖像上從原點開始滑動,計…

對MySQL性能影響關系緊密的五大配置參數

以下的文章主要是對MySQL性能影響關系緊密的五大配置參數的介紹,我前幾天在相關網站看見對MySQL性能影響關系緊密的五大配置參數的資料,覺得挺好,就拿出來供大家分享,望你能有所收獲。(一)連接 連接通常來自Web服務器,…

JAVA安裝作用_jdk安裝配置及其作用

2.安裝好了就是去配置路徑了,我的是win7系統,步驟如下:桌面上的計算機右擊-》高級系統設置—》環境變量-》系統變量-》新建一共要新建三個變量JAVA_HOME,PATH和CLASSPATH1>JAVA_HOME:(這么寫為了方便以后可能改動jdk的安裝路徑&#xff0c…

用C#開發Windows應用程序

To develop windows application, we need to using studio and follow some steps: 要開發Windows應用程序 ,我們需要使用studio并遵循一些步驟: Step 1) First of all we launch visual studio. 步驟1)首先,我們啟動Visual Studio。 Ste…

圖像分割——基于二維灰度直方圖的閾值處理

前言 像素灰度值僅僅反映了像素灰度級的幅值大小,并沒有反映出像素與鄰域的空間相關信息。 二維灰度直方圖的概念 二維灰度直方圖:像素的灰度值分布和鄰域的平均灰度值分布構成的二維直方圖 二維直方圖的值N(i,j) 。其中,if(x,y) 圖像(x,y…

多維角度聊聊結對編程

在敏捷軟件開發的各種實踐中,結對編程(Pair Programming,下文簡稱Pair)是特別有爭議的。Pair有一個特點,那就是還沒有進行過任何Pair實踐前,你很可能對它已經有了“喜歡” 或者是“討厭”的印象。如果有人問…

17-直方圖

直方圖 何為直方圖?沒那么高大上,其實就是二維統計圖。每個照片都是有像素點所組成,當然也是[0,255],直方圖就是統計每個值所對應的像素點有幾個。 直方圖橫坐標表示0-255這些像素點值;縱坐標表示對應像素點值的個數有…

java求水電費_java水電費管理系統

每天記錄學習,每天會有好心情。*^_^*今天和一個朋友共同完成了一個基于web的java水電費管理系統項目,我們在開發時選用的框架是SSM(MYECLIPSE)框架。我這個朋友知識有限,只會這個框架,哈哈,都是為了方便他。和往常一樣…

zemax微透鏡陣列示例_陣列反向! Ruby中的示例方法

zemax微透鏡陣列示例陣列反向! 方法 (Array reverse! Method) In this article, we will study about Array.reverse! method. You all must be thinking the method must be doing something related to reversing certain elements as we have done in the case o…

Opencv實戰【1】人臉檢測并對ROI區域進行部分處理(變身喬碧蘿!!!)

步驟: 1、利用Opencv自帶的分類器檢測人臉 預備知識:Haar特征分類器 Haar特征分類器就是一個XML文件,該文件中會描述人體各個部位的Haar特征值。包括人臉、眼睛、嘴唇等等。 Haar特征分類器存放地址: (找自己的安裝…

【黑馬甄選離線數倉day10_會員主題域開發_DWS和ADS層】

day10_會員主題域開發 會員主題_DWS和ADS層 DWS層開發 門店會員分類天表: 維度指標: 指標:新增注冊會員數、累計注冊會員數、新增消費會員數、累計消費會員數、新增復購會員數、累計復購會員數、活躍會員數、沉睡會員數、會員消費金額 維度: 時間維度&#xff08…

iPad和iPhone的app圖標尺寸、用途、設置方法

下面是在iPhone專用程序、iPad專用程序和通用程序中使用圖標文件的指導,由譯言網翻譯自蘋果官方文檔。原文 http://article.yeeyan.org/view/395/100567 注意:圖標是你的程序包所必需的組成部分。如果你沒有提供程 序所需的各種尺寸的圖標,系…