PHP字串長度找子字串與取代字串的常用函數

PHP在字串的操作上,有非常多的相關函數可以使用,我們在這裡只介紹經常會使用到的字串操作函數。像是如何取得字串的長度、在某一個字裏面尋找子字串首次出現的位置、以及字串的取代函數...等等。這些都是在編寫程式的時候,頻繁被程式設計師使用到的基礎函數。 取得字串長度 <?...

2012年7月11日 星期三

indexOf(String str,int fromIndex)方法 - java字串類別

回java字串常用函式主頁

indexOf(String str,int fromIndex)方法

給定起始位置,回傳在目前字串中第一次找到指定字串的索引位置。


範例程式


package jcode.test.string;

public class Sample005 {

  /**
   @param args
   */
  public static void main(String[] args) {
    String str = "abcdefg1263457";
    
    //起始位置
    int fromIndex = 2
    
    int idx = str.indexOf("cd",fromIndex);
    System.out.println("[給定起始位置找]idx值: "+idx);
  }

}


執行結果








indexOf(String str)方法 - java字串類別

回java字串常用函式主頁

indexOf(String str)方法

回傳在目前字串中第一次找到指定字串的索引位置。


範例程式

package jcode.test.string;

public class Sample004 {

  /**
   @param args
   */
  public static void main(String[] args) {
    String str = "abcdefg1263457";
    int idx = str.indexOf("cd");
    System.out.println("idx值: "+idx);
  }

}


執行結果








substring(int beginIndex, int endIndex)方法 - java字串類別

回java字串常用函式主頁

substring(int beginIndex, int endIndex)方法

回傳在目前字串中擮取的子字串,範圍從指定開始索引位置(beginIndex)到指定結尾索引位置(endIndex)。因此子字串長度必需小於或等於目前字串長度。


範例程式

package jcode.test.string;

public class Sample009 {

  /**
   @param args
   */
  public static void main(String[] args) {
    String str = "ab6cdefg1263457";  
    
    //起始位置
    int beginIndex = 3;
    //結束位置
    int endIndex = 4;
    
    String subStr = str.substring(beginIndex,endIndex);    
    System.out.println("[介於索引3~4]subStr子字串: "+subStr);

  }

}


執行結果








replace(char oldChar, char newChar)方法 - java字串類別

回java字串常用函式主頁

replace(char oldChar, char newChar)方法

將在目前字串中所有oldChar的字元取代為newChar字元。


範例程式

package jcode.test.string;

public class Sample010 {

  /**
   @param args
   */
  public static void main(String[] args) {
    String str = "abcdefg123456789";
    
    //舊的被取代字串
    String oldChar = "abcdefg";
    //新的取代字串
    String newChar = "00000";
    
    String newString = str.replace(oldChar, newChar);
    System.out.println("newString新字串: "+newString);
  }

}


執行結果








chartAt(int index)方法 - java字串類別

回java字串常用函式主頁

chartAt(int index)方法

回傳特定索引char值。特定索引範圍介於0到length() - 1。
在序列中的第一個char值是位於索引0的位置,下一個值於索引1的位置,以此類推。
假若特定索引位置的char值是一個代理值(Unicode字元)時,則返回Unicode對映回來的代表值。


範例程式

package jcode.test.string;

public class Sample001 {

  /**
   @param args
   */
  public static void main(String[] args) {
    String str = "abcdefg1263457";
    char c3 = str.charAt(3);
    System.out.println("索引3的值: "+c3);
  }

}


執行結果

 



endsWith(String suffix)方法 - java字串類別

回java字串常用函式主頁

endsWith(String suffix)方法

測試在目前字串中從尾部為起始是否有後綴。


範例程式

package jcode.test.string;

public class Sample003 {

  /**
   @param args
   */
  public static void main(String[] args) {
    String str = "abcdefg1263457";
    boolean flag = str.endsWith("7");
    if(flag){
      System.out.println("字串'7'找到了!");
    }else{
      System.out.println("找不到資料。");
    }

  }

}


執行結果








startsWith(String prefix)方法 - java字串類別

回java字串常用函式主頁

startsWith(String prefix)方法

測試在目前字串中是否有字首。


範例程式

package jcode.test.string;

public class Sample002 {

  /**
   @param args
   */
  public static void main(String[] args) {
    String str = "abcdefg1263457";
    boolean flag = str.startsWith("a");
    if(flag){
      System.out.println("字串'a'找到了!");
    }else{
      System.out.println("找不到資料。");
    }

  }

}


執行結果