20180912-Java实例02

Java 实例 – 删除字符串中的一个字符


以下实例中我们通过字符串函数 substring() 函数来删除字符串中的一个字符,我们将功能封装在 removeCharAt 函数中。

// Main.java 文件

public class Main{
public static void main(String[] args){
String str = "this is Java";
System.out.println(removeCharAt(str,3));
}

public static String removeCharAt(String s,int pos){
return s.substring(0,pos) + s.substring(pos +1);
}
}

以上命令执行结果如下:


thi is Java


---------------------------------------------------------------

Java 实例 – 字符串替换

如何使用java替换字符串中的字符呢?


以下实例中我们使用 java String 类的 replace 方法来替换字符串中的字符:

public class StringReplaceEmp{
public static void main(String args[]){
String str = "Hello World";
System.out.println(str.replace('H','W'));
System.out.println(str.replaceFirst("He","Wa"));
System.out.println(str.replaceAll("He","Ha"));
}
}

以上代码实例输出结果为:


Wello World
Wallo World
Hallo World

------------------------------------------------------------------


Java 实例 – 字符串反转


以下实例演示了如何使用 Java 的反转函数 reverse() 将字符串反转:


public class StringReverseExample{
public static void main(String[] args){
String string = "abcdef";
String reverse = new StringBuffer(string);
reverse().toString();
System.out.println("String before reverse:"+string);
System.out.println("String after reverse:"+reverse);
}
}


以上代码实例输出结果为:


String before reverse:abcdef
String after reverse:fedcba

----------------------------------------------------------------

Java 实例 – 字符串查找


以下实例使用了 String 类的 indexOf() 方法在字符串中查找子字符串出现的位置,如过存在返回字符串出现的位置(第一位为0),如果不存在返回 -1:

//SearchStringEmp.java 文件

public class SearchStringEmp{
public static void main(Stirng[] args){
String strOrig = "Hello readers";
int intIndex = strOrig.indexOf("Hello");

if(intIndex == -1)}{
System.out.println("Hello not found");
}else{
System.out.println("Found Hello at index" + intIndex);
}
}
}

以上代码实例输出结果为:


Found Hello at index 0

-------------------------------------------------------------


Java 实例 – 字符串分割


以下实例使用了 split(string) 方法通过指定分隔符将字符串分割为数组:


//JavaStringSplitEmp.java 文件 ??

public class JavaStringSplitEmp{
public static void main(String[] args){
String str = "jan-feb-march";
String[] temp;
String delimeter = "-";

temp = str.split(delimeter);
for(int i=0;i<temp.length;i++){
System.out.println(temp[i]);
System.out.println("");
str = "jan.feb.march";
delimeter = "\\";
temp = str.split(delimeter);
}
for(int i=0;i<temp.length;i++){
System.out.println(temp[i]);
System.out.println("");
temp = str.split(delimeter,2);
for(int j =0; j<temp.lenth;j++){
System.out.println(temp[i]);
}
}

}
}


以上代码实例输出结果为:


jan

feb

march

jan

jan
jan
feb.march

feb.march
feb.march

Java 实例 – 字符串小写转大写


以下实例使用了 String toUpperCase() 方法将字符串从小写转为大写:


// StringToUpperCaseEmp.java 文件

public class StringToUpperCaseEmp{
public static void main(String[] args){
String str = "string abc touppercase";
String strUpper = str.toUpperCase();
System.out.println("Original String:" +str);
System.out.println("String changed to upper case:" +strUpper);
}
}


以上代码实例输出结果为:


Original String: string abc touppercase
String changed to upper case: STRING ABC TOUPPERCASE


-----------------------------------------------------------------

Java 实例 – 测试两个字符串区域是否相等


以下实例使用了 regionMatches() 方法测试两个字符串区域是否相等:


//StringRegioMatch.java 文件

public class StringRegioMatch{
public static void main(String[] args){
String first_str = "Welcome to Microsoft";
String second_str = "I work with microsoft";

boolean match1 = first_st.regionMatches(11,seconde_str,12,9);
boolean match2 = first_str.regionMatches(true,11,second_str,12,9);

System.out.println("区分大小写返回值:" +match1);
System.out.println("不区分大小写返回值:" +match2);

}
}


first_str.regionMatches(11, second_str, 12, 9) 表示将 first_str 字符串从第11个字符"M"开始和 second_str 字符串的第12个字符"M"开始逐个比较,共比较 9 对字符,由于字符串区分大小写,所以结果为false。


如果设置第一个参数为 true ,则表示忽略大小写区别,所以返回 true。


以上代码实例输出结果为:


区分大小写返回值:false
不区分大小写返回值:true

猜你喜欢

转载自www.cnblogs.com/Alanf/p/9636566.html