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

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

顯示具有 Java字串使用 標籤的文章。 顯示所有文章
顯示具有 Java字串使用 標籤的文章。 顯示所有文章

2012年7月13日 星期五

split(String regex)方法 - java字串類別

回java字串常用函式主頁

split(String regex)方法

以給定的正則表示式(regular expression)來分割目前字串。


範例程式

package jcode.test.string;

public class Sample020 {

  /**
   @param args
   */
  public static void main(String[] args) {
    String str = "first,second,third";
    String[] ss = str.split(",");
    for(int i=0;i<ss.length;i++){
      System.out.println(ss[i]);      
    }
    
  }

}


執行結果

以「,」為分隔符號,來切割目前字串為first與second和third三部份








toUpperCase()方法 - java字串類別

回java字串常用函式主頁

toUpperCase()方法

將目前的字串轉換為大寫字元。


範例程式

package jcode.test.string;

public class Sample019 {

  /**
   @param args
   */
  public static void main(String[] args) {
    String str = "so far so good.";
    System.out.println("str字串轉大寫: "+str.toUpperCase());

  }

}


執行結果







toLowerCase()方法 - java串字類別

回java字串常用函式主頁

toLowerCase()方法

將目前的字串轉換為小寫字元。


範例程式

package jcode.test.string;

public class Sample018 {

  /**
   @param args
   */
  public static void main(String[] args) {
    String str = "GOOD JOB!";
    System.out.println("str字串轉小寫: "+str.toLowerCase());

  }

}


執行結果







trim()方法 - java串字類別

回java字串常用函式主頁

trim()方法

去除目前字串的前後空白字元。


範例程式

package jcode.test.string;

public class Sample017 {

  /**
   @param args
   */
  public static void main(String[] args) {
    String str = "  abc13535  ";
    System.out.println("[去除空白]str字串: ");
    System.out.println(str.trim());

  }

}


執行結果









isEmpty()方法 - java字串類別

回java字串常用函式主頁

isEmpty()方法

用來判斷目前的字串是否為空值,與目前字串的length()等於0為等價的判斷。


範例程式

package jcode.test.string;

public class Sample016 {

  /**
   @param args
   */
  public static void main(String[] args) {
    String str = "";
    if(str.isEmpty()){
      System.out.println("str為空字串");
    }

  }

}


執行結果








length()方法 - java字串類別

回java字串常用函式主頁

length()方法

回傳目前字串的長度。
長度等同於在目前字串中的Unicode單位的數量。


範例程式

package jcode.test.string;

public class Sample015 {

  /**
   @param args
   */
  public static void main(String[] args) {
    String str = "abcde12345";
    System.out.println("str字串長度: "+str.length());

  }

}


執行結果








equalsIgnoreCase(String anotherString)方法 - java串字類別

回java字串常用函式主頁

equalsIgnoreCase(String anotherString)方法

比較目前的字串和指定字串是否相等,但勿略字母的大小寫。
兩字串擁有相同的長度與相同的循序字元,且不計大小寫。


範例程式

package jcode.test.string;

public class Sample014 {

  /**
   @param args
   */
  public static void main(String[] args) {
    String str = "abc123";
    String anObject = "ABC123";
    if(str.equalsIgnoreCase(anObject)){
      System.out.println("[忽略大小寫]str與anObject兩字串相等");
    }

  }

}


執行結果








equals(Object anObject)方法 - java字串類別

回java字串常用函式主頁

equals(Object anObject)方法

比較目前的字串和指定字串是否相等,切記字串相等比較勿使用「= =」運算子。
若指定的字串不為空值而且它的循序字元和目前字串相同,則回來真值(true)。


範例程式

package jcode.test.string;

public class Sample013 {

  /**
   @param args
   */
  public static void main(String[] args) {
    String str = "abc123";
    String anObject = "abc123";
    if(str.equals(anObject)){
      System.out.println("str與anObject兩字串相等");
    }

  }

}


執行結果








2012年7月11日 星期三

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

回java字串常用函式主頁

lastIndexOf(String str)方法

在目前字串中,從尾部開始找出給定字串第一次出現的索引位置。
空子串通常表示目前的字串長度為0


範例程式

package jcode.test.string;

public class Sample006 {

  /**
   @param args
   */
  public static void main(String[] args) {
    String str = "ab6cdefg1263457";    
    int idx = str.lastIndexOf("6");
    System.out.println("[右邊開始找]idx值: "+idx);

  }

}


執行結果








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

回java字串常用函式主頁

lastIndexOf(String str, int fromIndex)方法

在目前字串中,從尾部特定的索引位置開始找出給定字串第一次出現的索引位置。


範例程式


package jcode.test.string;

public class Sample007 {

  /**
   @param args
   */
  public static void main(String[] args) {
    String str = "ab6cdefg1263457";    
    
    //起始位置
    int fromIndex = 11;
    
    int idx = str.lastIndexOf("6",fromIndex);
    System.out.println("[給定起始位置,右邊開始找'6']idx值: "+idx);
  }

}


執行結果








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

回java字串常用函式主頁

substring(int beginIndex)方法

回傳在目前字串中擮取的子字串,範圍從給定開始索引位置延伸到字串結尾。


範例程式

package jcode.test.string;

public class Sample008 {

  /**
   @param args
   */
  public static void main(String[] args) {
    String str = "ab6cdefg1263457";  
    String subStr = str.substring(10);
    
    System.out.println("[從索引10到字串結尾]subStr子字串: "+subStr);
  }

}


執行結果








getBytes()方法 - java字串類別

回java字串常用函式主頁

getBytes()方法

採用目前作業系統安裝的編碼方式,將目前的字串編碼為循序的bytes陣列。


範例程式

package jcode.test.string;

public class Sample011 {

  /**
   @param args
   */
  public static void main(String[] args) {
    String str = "address000012345";
    byte[] b =  str.getBytes();
    
    System.out.println("b[0]: "+b[0]);
  }

}


執行結果

7位元Ascii 編碼的英文字母小寫「a」就是十進元數字97







getBytes(String charsetName)方法 - java字串類別

回java字串常用函式主頁

getBytes(String charsetName)方法

採用指定編碼方式,將目前的字串編碼為循序的bytes陣列。


範例程式

package jcode.test.string;

import java.io.UnsupportedEncodingException;

public class Sample012 {

  /**
   @param args
   @throws UnsupportedEncodingException 
   */
  public static void main(String[] argsthrows UnsupportedEncodingException {
    String str = "目的地址address";
    
    //指定編碼
    String charsetName = "BIG5";
    
    byte[] b =  str.getBytes(charsetName);    
    System.out.println("b[0]: " + b[0]);
  }

}


執行結果








總結:一般在開發jsp網頁常遇到亂碼的處理方式就是將字串轉成byte且指定編碼方式,
經常使用「new String(str.getBytes("8859_1","BIG5"));」此段語法,來解決編碼轉換錯誤的問題。

例子:  String str = request.getParameter("param1");
              String s = new String(str.getBytes("8859_1","BIG5"));



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("找不到資料。");
    }

  }

}


執行結果