1.java.lang.String
2.""括起來的字串(變數)會自動建立String物件
程式
/**
*
*/
/**
* @author vicyuyu311
*
*/
public class usestring {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1 = "Hello Java String1!";
System.out.println(s1);
String s2;
s2 = "Hello Java String2!";
System.out.println(s2);
}
}
sample041 連結String字串
1.使用String類別的concat
2.使用 + 運算子(不過有小問題)
程式
/**
*
*/
/**
* @author vicyuyu311
*
*/
public class connectstring {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//使用concat方法
String s1 = "Hello Java ";
String s2 = "String1!";
s1 = s1.concat(s2);
System.out.println(s1);
//使用+運算子
String s3 = "Hello Java ";
String s4 = "String2!";
String s5 = "s3 + s4";
System.out.println(s5);
}
}
sample042 取得String字串的長度
程式
/**
*
*/
/**
* @author vicyuyu311
*
*/
public class stringlength {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = "Hello Java String!";
int len = s.length();
System.out.print(s + " 的長度是 ");
System.out.println(len + "個字");
}
}
sample043 取得string字串的一部分
1.String substring(int beginindex)
2.String substring(int beginindex,int endindex)
程式
/**
*
*/
/**
* @author vicyuyu311
*
*/
public class substring {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1 = "Hello Java String!";
//取出第7個字之後的部分
String s2 = s1.substring(5);
System.out.println(s2);
//取出位置為7~10之間的字
String s3 = s1.substring(5,10);
System.out.println(s3);
}
}
sample044 在String字串中搜尋字串
1.int indexof(string) -傳回指定文字第一次出現的位置
2.int indexof(string,int fromIndex) -從fromIndex指定位置開始搜尋,傳回指定文字第一次出現的位置
3.int lastIndexof(strings) -傳回指定文字最後一次出現的位置
4.int lastIndexof(strings,int fromIndex)-從指定的位置開始往回搜尋,傳回指定文字最後出現的位置
5.char使用單引號string使用雙引號
程式
/**
*
*/
/**
* @author vicyuyu311
*
*/
public class inlastindexof {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = "Hello Java String!";
//求得字元'o'第一次出現的位置
System.out.println(s.indexOf("llo"));
//從第9個字元開始,搜尋'a'第一次出現的位置
System.out.println(s.indexOf('a',8));
//如果指定的文字不存在,則傳回-1
System.out.println(s.indexOf('z'));
//傳回指定文字最後一次出現的位置
System.out.println(s.lastIndexOf('l'));
//從指定的位置開始往回搜尋,傳回指定文字最後出現的位置
System.out.println(s.lastIndexOf('l',4));
}
}
sample045 在String字串中取代字串
1.String replace(char oldchar,char newchar)
程式
/**
*
*/
/**
* @author vicyuyu311
*
*/
public class replace {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = "Hello Java String!";
s = s.replace('a','A');
System.out.println(s);
}
}
sample046 轉換String字串的大小寫
1.使用string類別的toLowerCase轉小寫
2.使用string類別的toUpperCase轉大寫
程式
/**
*
*/
/**
* @author vicyuyu311
*
*/
public class toupperlowercase {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = "Hello Java String!";
System.out.println(s.toLowerCase());
System.out.println(s.toUpperCase());
}
}
sample047 刪除string字串前後的空白字元
1.使用string類別的trim方法
程式
/**
*
*/
/**
* @author vicyuyu311
*
*/
public class trim {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = " Hello Java String! ";
System.out.println(s + " : 長度為 " + s.length() );
//刪除前後的空白
s = s.trim();
System.out.println(s + " :長度為 " + s.length());
}
}
sample048 比較兩個string字串
1.使用string的equal方法
程式
/**
*
*/
/**
* @author vicyuyu311
*
*/
public class compare2string {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1 = "Hello Java String!";
String s2 = "Hello World!";
//String 物件得比較
System.out.print(s1 + "和" + s2);
if (s1.equals(s2)) {
System.out.println(" 是相同字串 ");
} else {
System.out.println(" 是不同的字串");
}
//比較引號括起來的字串
System.out.print("Hello World!" + " 和 " + s2);
if ("Hello World!".equals(s2)) {
System.out.println(" 是相同的字串 ");
} else {
System.out.println("是不同的字串");
}
}
}
sample049 比較兩個string字串並忽略大小寫
1.使用string類別的equals方法
程式
/**
*
*/
/**
* @author vicyuyu311
*
*/
public class equalsIgnorecase {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1 = "Hello Java String!";
String s2 = "hello java string!";
//equals方法會認定不同字串
System.out.print(s1 + "和" + s2);
if (s1.equals(s2)) {
System.out.println(" 是相同的字串");
} else {
System.out.println(" 是不同的字串");
//equalsIgnoreCase方法會認定為相同的字串,忽略大小寫
System.out.print(s1 + " 和 " + s2);
if (s1.equalsIgnoreCase(s2)) {
System.out.println(" 是相同的字串");
} else {
System.out.println(" 是不同的字串");
}
}
}
}
sample050 複製string字串
1.使用tostring方法可以複製字串
程式
/**
*
*/
/**
* @author vicyuyu311
*
*/
public class tostring {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1 = "Hello Java String!";
String s2 = s1.toString();
System.out.println(s2);
}
}
沒有留言:
張貼留言