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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Bash字符串处理(与Java对照) - 19.查找字符的位置

發布時間:2023/12/9 java 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Bash字符串处理(与Java对照) - 19.查找字符的位置 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

From:?http://codingstandards.iteye.com/blog/1198917

In Java

String.indexOf & String.lastIndexOf

?int ??? indexOf(int ch)
????????? 返回指定字符在此字符串中第一次出現處的索引。
?int ??? indexOf(int ch, int fromIndex)
????????? 從指定的索引開始搜索,返回在此字符串中第一次出現指定字符處的索引。

?

int ??? lastIndexOf(int ch)
????????? 返回最后一次出現的指定字符在此字符串中的索引。
?int ??? lastIndexOf(int ch, int fromIndex)
????????? 從指定的索引處開始進行后向搜索,返回最后一次出現的指定字符在此字符串中的索引。

?

StringUtils.indexOf & StringUtils.indexOfAny & StringUtils.indexOfIgnoreCase & StringUtils.lastIndexOf

在 org.apache.commons.lang.StringUtils 中提供了很多查找字符索引的方法,包括正向和反向。

?

static int ??? indexOf(String str, char searchChar)
????????? Finds the first index within a String, handling null.
static int ??? indexOf(String str, char searchChar, int startPos)
????????? Finds the first index within a String from a start position, handling null.

static int ??? lastIndexOf(String str, char searchChar)
????????? Finds the last index within a String, handling null.
static int ??? lastIndexOf(String str, char searchChar, int startPos)
????????? Finds the last index within a String from a start position, handling null.

?

在 org.apache.commons.lang.StringUtils 中還提供了查找任意字符出現的位置的方法。
static int ??? indexOfAny(String str, char[] searchChars)
????????? Search a String to find the first index of any character in the given set of characters.
static int ??? indexOfAny(String str, String searchChars)
????????? Search a String to find the first index of any character in the given set of characters.
static int ??? indexOfAnyBut(String str, char[] searchChars)
????????? Search a String to find the first index of any character not in the given set of characters.
static int ??? indexOfAnyBut(String str, String searchChars)
????????? Search a String to find the first index of any character not in the given set of characters.

?

In Bash

使用遍歷字符的方式來查找字符的位置

函數:strchr <str> <ch>

如果找到,打印字符的位置,從0開始計數,退出碼為0;否則打印-1,退出碼為1

Bash代碼??
  • strchr(){??
  • ????local?i??
  • ????for?((i=0;?i<${#1};?++i))??
  • ????do??
  • ????????if?[?"${1:i:1}"?==?"$2"?];?then??
  • ????????????echo?$i??
  • ????????????return?0??
  • ????????fi??
  • ????done??
  • ????echo?-1??
  • ????return?1??
  • }??
  • ?

    [root@web ~]#?STR=123456789?
    [root@web ~]#?CH=6?
    [root@web ~]#?strchr "$STR" "$CH"?
    5
    [root@web ~]#?echo $??
    0
    [root@web ~]#?CH=a?
    [root@web ~]#?strchr "$STR" "$CH"?
    -1
    [root@web ~]#?echo $??
    1
    [root@web ~]#

    ?

    用expr index來查找字符的位置

    格式:expr index "$STR" "$CHARS"

    在STR中查找CHARS中的任何字符(而不是子串),打印第一個位置。

    注意:返回的下標是從1開始的,0表示沒有找到。

    不完全對應于Java的indexOf方法。倒是與C++ STL string的find_first_of相似。

    ?

    man expr 寫道 index STRING CHARS
    ?? index in STRING where any CHARS is found, or 0

    ?

    [root@jfht ~]#?STR="Hello World"?
    [root@jfht ~]#?SUB="l"?
    [root@jfht ~]#?expr index "$STR" "$SUB"?
    3

    [root@jfht ~]#?SUB="not found"??????# 注意,expr index并不能用能查找子串的位置,而是該字符串中任何字符首次出現的位置?
    [root@jfht ~]#?expr index "$STR" "$SUB"?
    5

    ?

    用awk index來查找字符出現的位置

    格式1:awk -v "STR=$STR" -v "CH=$CH" '{print index(STR,CH)}' <<<""

    格式2:echo | awk -v "STR=$STR" -v "CH=$CH" '{print index(STR,CH)}'

    因為awk默認會從標準輸入讀取數據,所以必須進行輸入的重定向。

    此方法不僅可以查詢字符的出現位置,也可以查詢子串的出現位置。但不能查找任意字符的出現位置。

    注意:索引位置從1開始計數,0表示沒有找到。

    man awk 寫道 index(s, t) Returns the index of the string t in the string s, or 0 if t is not present. (This
    implies that character indices start at one.) ?

    [root@web ~]#?STR=123456789?
    [root@web ~]#?CH=6

    [root@web ~]#?awk -v "STR=$STR" -v "CH=$CH" '{print index(STR,CH)}' <<<""?
    6
    [root@web ~]#?echo | awk -v "STR=$STR" -v "CH=$CH" '{print index(STR,CH)}'?
    6
    [root@web ~]#


    總結

    以上是生活随笔為你收集整理的Bash字符串处理(与Java对照) - 19.查找字符的位置的全部內容,希望文章能夠幫你解決所遇到的問題。

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