国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

字符串分割(split),將字符串按照指定字符進(jìn)行分割。split(String regex)和split(String regex, int limit)

這篇具有很好參考價值的文章主要介紹了字符串分割(split),將字符串按照指定字符進(jìn)行分割。split(String regex)和split(String regex, int limit)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報違法"按鈕提交疑問。

一、split(String regex)字符串分割,將字符串按照指定字符進(jìn)行分割,返回的是一個字符串?dāng)?shù)組。

  public String[] split(String regex) {
        return split(regex, 0);
    }
原理:參數(shù)名稱是regex表示的是以某個字符串進(jìn)行字符分割。

值得注意的是Java中使用String.split對有些特殊字符進(jìn)行分割時需要進(jìn)行轉(zhuǎn)義才能進(jìn)行分割。

例如:“| " ,”*", ". "等,否則無法分割會報錯。

實例1:根據(jù)空格切割
    public static void main(String[] args) {
        String k1 = "招標(biāo)人 投標(biāo)人 設(shè)計單位 施工單位";
        String[] k = k1.split(" ");
        System.out.println(k[0]);
        System.out.println(k[1]);
        System.out.println(k[2]);
        System.out.println(k[3]);
    }
輸出結(jié)果:

字符串分割(split),將字符串按照指定字符進(jìn)行分割。split(String regex)和split(String regex, int limit)

實例2:根據(jù)特殊字符進(jìn)行“.”分割
    public static void main(String[] args) {
        String k1 = "招標(biāo)人.投標(biāo)人.設(shè)計單位.施工單位";
        String[] k = k1.split("\\.");//需要轉(zhuǎn)義
        System.out.println(k[0]);
        System.out.println(k[1]);
        System.out.println(k[2]);
        System.out.println(k[3]);
    }
輸出結(jié)果:

字符串分割(split),將字符串按照指定字符進(jìn)行分割。split(String regex)和split(String regex, int limit)

二、split(String regex, int limit)字符串分割,返回的是字符串?dāng)?shù)組。

 public String[] split(String regex, int limit) {
        /* fastpath if the regex is a
         (1)one-char String and this character is not one of the
            RegEx's meta characters ".$|()[{^?*+\\", or
         (2)two-char String and the first char is the backslash and
            the second is not the ascii digit or ascii letter.
         */
        char ch = 0;
        if (((regex.value.length == 1 &&
             ".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) ||
             (regex.length() == 2 &&
              regex.charAt(0) == '\\' &&
              (((ch = regex.charAt(1))-'0')|('9'-ch)) < 0 &&
              ((ch-'a')|('z'-ch)) < 0 &&
              ((ch-'A')|('Z'-ch)) < 0)) &&
            (ch < Character.MIN_HIGH_SURROGATE ||
             ch > Character.MAX_LOW_SURROGATE))
        {
            int off = 0;
            int next = 0;
            boolean limited = limit > 0;
            ArrayList<String> list = new ArrayList<>();
            while ((next = indexOf(ch, off)) != -1) {
                if (!limited || list.size() < limit - 1) {
                    list.add(substring(off, next));
                    off = next + 1;
                } else {    // last one
                    //assert (list.size() == limit - 1);
                    list.add(substring(off, value.length));
                    off = value.length;
                    break;
                }
            }
            // If no match was found, return this
            if (off == 0)
                return new String[]{this};

            // Add remaining segment
            if (!limited || list.size() < limit)
                list.add(substring(off, value.length));

            // Construct result
            int resultSize = list.size();
            if (limit == 0) {
                while (resultSize > 0 && list.get(resultSize - 1).length() == 0) {
                    resultSize--;
                }
            }
            String[] result = new String[resultSize];
            return list.subList(0, resultSize).toArray(result);
        }
        return Pattern.compile(regex).split(this, limit);
    }
原理:split(String regex, int limit)字符串中的regex為非必填項,用來表示用于分割的是一個字符還是多個字符,如果忽略該選項,返回包含整個字符串的單一元素數(shù)組。Limit可選項,該值用來限制返回中的元素個數(shù)。

注意:特殊字符需要進(jìn)行轉(zhuǎn)義。

例如:“| " ,”*", ". "等,否則無法分割會報錯。

實例1:當(dāng)regex為空字符情況。
    public static void main(String[] args) {
        String k1 = "招標(biāo)人#投標(biāo)人#設(shè)計單位#施工單位";
        String[] k = k1.split("",4);//需要轉(zhuǎn)義
        System.out.println(k[0]);
        System.out.println(k[1]);
        System.out.println(k[2]);
        System.out.println(k[3]);
    }
運(yùn)行結(jié)果:

字符串分割(split),將字符串按照指定字符進(jìn)行分割。split(String regex)和split(String regex, int limit)

實例2:當(dāng)regex為“#”號字符情況。
    public static void main(String[] args) {
        String k1 = "招標(biāo)人#投標(biāo)人#設(shè)計單位#施工單位";
        String[] k = k1.split("#",4);//需要轉(zhuǎn)義
        System.out.println(k[0]);
        System.out.println(k[1]);
        System.out.println(k[2]);
        System.out.println(k[3]);
    }
運(yùn)行結(jié)果:

字符串分割(split),將字符串按照指定字符進(jìn)行分割。split(String regex)和split(String regex, int limit)

實例3:當(dāng)regex為多字符的情況
    public static void main(String[] args) {
        String k1 = "招標(biāo)人#投標(biāo)人#設(shè)計單位#施工單位";
        String[] k = k1.split("#投標(biāo)人",4);//需要轉(zhuǎn)義
        System.out.println(k[0]);
        System.out.println(k[1]);
    }
運(yùn)行結(jié)果:

字符串分割(split),將字符串按照指定字符進(jìn)行分割。split(String regex)和split(String regex, int limit)

實例4:當(dāng)regex為“#”字符且limit限制返回2個的情況
   public static void main(String[] args) {
        String k1 = "招標(biāo)人#投標(biāo)人#設(shè)計單位#施工單位";
        String[] k = k1.split("#",2);//需要轉(zhuǎn)義
        System.out.println(k[0]);
        System.out.println(k[1]);
    }
運(yùn)行結(jié)果:

字符串分割(split),將字符串按照指定字符進(jìn)行分割。split(String regex)和split(String regex, int limit)文章來源地址http://www.zghlxwxcb.cn/news/detail-502695.html

到了這里,關(guān)于字符串分割(split),將字符串按照指定字符進(jìn)行分割。split(String regex)和split(String regex, int limit)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點(diǎn)擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • python用split多字符分割字符串的方法

    1.python 有內(nèi)置函數(shù)split()分隔字符串,但這個內(nèi)置函數(shù)只能識別單個分隔符。 調(diào)用方法如下:? 其中:? str -- 分隔符,默認(rèn)為所有的空字符,包括空格、換行(n)、制表符(t)等。 num -- 分割次數(shù)。默認(rèn)為 -1, 即分隔所有。 示例如下: 2. 用正則表達(dá)式re模塊的split()函數(shù)可以使用

    2024年02月07日
    瀏覽(19)
  • vue使用split()將字符串分割數(shù)組join()將數(shù)組轉(zhuǎn)字符串reverse()將數(shù)組反轉(zhuǎn)

    1.split() 將字符串切割成數(shù)組 輸出如下 1.split()不傳參數(shù)默認(rèn)整個字符串作為數(shù)組的一個元素,返回包含原始字符串的數(shù)組 2.split(‘’)單引號不傳參數(shù)默認(rèn)將字符串拆分成一個個字符數(shù)組 如輸入?yún)?shù): const str = 123456789’ 拆分后:[‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’

    2023年04月08日
    瀏覽(22)
  • Java中String字符串截取幾種方法(substring,split)

    Java中String字符串截取幾種方法(substring,split)

    這是一個Java中的String的基礎(chǔ)用法的演示。 下面通過代碼對大家進(jìn)行講解 substring 這里用來ndexOf,lastIndexOf這兩個函數(shù)進(jìn)行字符定位,一個是從前往后尋找第一個,一個是從后往前尋找第一個。 split split本身就是分割的意思,里面?zhèn)魅胍粋€字符串,通過這個字符串進(jìn)行分割,也是

    2024年02月05日
    瀏覽(23)
  • String字符串分割的3種方法 Java

    使用了 split(string) 方法通過指定分隔符將字符串分割為數(shù)組 在java.lang包中有String.split()方法,返回是一個數(shù)組?!?”和“|”都是轉(zhuǎn)義字符,必須得加\\\"“; 如果用“.”作為分隔的話,必須是如下寫法:String.split(”.“),這樣才能正確的分隔開,不能用String.split(”.“); 如果用“

    2024年01月24日
    瀏覽(22)
  • 使用v-html進(jìn)行渲染如何使標(biāo)簽按照字符串顯示,特殊樣式標(biāo)簽(自己添加的部分)按照標(biāo)簽解析

    使用v-html進(jìn)行渲染如何使標(biāo)簽按照字符串顯示,特殊樣式標(biāo)簽(自己添加的部分)按照標(biāo)簽解析 需求:是內(nèi)容里如果含有含有www.baidu.com這種鏈接高亮顯示 解決辦法: 首先拿到需要回顯的內(nèi)容content對內(nèi)容進(jìn)行轉(zhuǎn)義escapeHTML,之后再去判斷是否是鏈接進(jìn)行高亮添加 代碼如下

    2024年02月16日
    瀏覽(32)
  • Python 隨機(jī)輸入一個字符串,統(tǒng)計該字符串中各種字符出現(xiàn)的次數(shù),并將統(tǒng)計結(jié)果按照字符出現(xiàn)次數(shù)從高到低進(jìn)行排序,最終打印排序后的信息
  • C# 將字符串進(jìn)行分割

    將字符串str1 = \\\"select * from table \\\"; 進(jìn)行分割,有兩種方法: 用字符分割字符串: 字符串.split(); 用字符串分割字符串: Regex.Split(); 兩者適用情況不同,詳細(xì)解釋如下: 1.1 單個字符 作為分割,使用.split(\\\'\\\') 1.2 多個字符 作為分割 ,使用.split(new char[]{\\\'\\\'}) 注: string[] arr = str.Spli

    2023年04月11日
    瀏覽(17)
  • Java中split分割帶空格的字符串 \t \n \r \f 及 \s 的區(qū)別

    t:制表符,相當(dāng)于tab n:換行 r:回車 f:換頁 s:在java正則表達(dá)式常見,例如java的匹配、替換、分割字符串(matches,split) 例: \\\"Java is fun\\\".matches(\\\"Java.*\\\") //返回true 二、正確使用split來分割空白字符 輸入:hello my ?lovely ? ?world! ? ? ? (分別為一個空格、兩個空格 和 一個制

    2024年02月15日
    瀏覽(23)
  • 如何進(jìn)行字符串的分割和拼接?

    字符串的分割和拼接是在C語言編程中常見的操作,尤其在處理文本數(shù)據(jù)時非常重要。在本文中,我將詳細(xì)解釋如何在C語言中進(jìn)行字符串的分割和拼接,包括使用標(biāo)準(zhǔn)庫函數(shù)和手動操作字符串?dāng)?shù)組的方法,以及一些實際應(yīng)用示例。 字符串分割是將一個字符串拆分成多個子字符

    2024年02月09日
    瀏覽(18)
  • C語言中的strtok()函數(shù)進(jìn)行字符串分割

    C語言中的strtok()函數(shù)進(jìn)行字符串分割

    引言 ????????在處理文本或字符串?dāng)?shù)據(jù)時,我們常常需要將一長串連續(xù)的字符按照特定的分隔符分解成一個個獨(dú)立的子串。C語言中提供了一個非常實用的庫函數(shù)—— strtok() ,用于實現(xiàn)這一功能。本文將通過一段示例代碼詳細(xì)解析并演示如何使用 strtok() 函數(shù)進(jìn)行字符串分

    2024年01月23日
    瀏覽(27)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包