日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

JAVA和javascrito_JAVA 和JavaScript的split方法异同

發布時間:2023/12/1 javascript 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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);

}

總結

以上是生活随笔為你收集整理的JAVA和javascrito_JAVA 和JavaScript的split方法异同的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。