字符串的截取方法 字符串轉換方法


package test1;
/*
* String當中與獲取相關的常用方法有:
* public int length(),獲取字符串當中含有的字符個數,拿到字符長度;
* public String concat(String str),將當前字符串和參數字符串拼接成為返回值新的字符串。
* public char charAt(int index),獲取指定索引位置的單個字符(索引從0開始)
* public int indexof(String str),查找餐宿字符串在本字符串當中首次出現的索引值,如果沒有返回-1值
*
* */
public class test1{
public static void main(String[] args) {
int length="adsfasfasdfadsf".length();
System.out.println(length);
// 拼接字符串
String str1="Hallo";
String str2="World";
System.out.println(str1.concat(str2));
System.out.println("***********************");
//獲取指定索引位置的單個字符
char ch="hallo".charAt(2);
System.out.println(ch);
System.out.println("***********************");
// 查找參數字符串在本來字符串當中出現的第一次索引位置;
// 如果根本 沒有返回值則返回-1
String origin="halloworldhalloworld";
int n=origin.indexOf('e');
System.out.println(n);//1

}
}

字符串截取方法
String c = "abc".substring(2,3);
     String d = cde.substring(1, 2);
字符串轉換方法

 

char[] toCharArray()
          將此字符串轉換為一個新的字符數組。

轉換為字節數組
public byte【】getByte();
轉化為ASCII值

public String replace(charSquence oldString,charSquency newString);


package test1;

import java.lang.CharSequence;

public class test1{
public static void main(String[] args){
String str="happy";
String str1="happy2";

byte[]num=str.getBytes();
System.out.println(str);
for (int i=0;i<num.length-1;i++)
{
System.out.println(num[i]);
}
// charsquence 可以接受字符串類型
String a=str.replace( "a","mmmm");
System.out.println(a); //字符串的字符變換
}
}

字符串分割方法
public String【】 split(String regex),按照參數的規則,將字符串划分為若干個部分
regex 可以是: : , 。













































免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM