截取字符串,并排序

话不多说,直接看代码:

public static void main(String[] args) {

    String test = "asdgsdgdf?b=asd&d=wer&g=zdf&a=gkj&p=hkl&m=jhg";
    String result = null;
    //截取?之前的,包括?
    String aa = test.substring(0, test.indexOf("?") + 1);
    //?之后的字符串
    result = test.substring(test.indexOf("?")+1);
    //遇到&就截取字符串
    String[] strs = result.split("&");
    //定义一个临时变量
    String temp = null;
    for (int i = 0; i < strs.length; i++) {
        //截取=之前的字符放入数组中
        int indexI = (int) (strs[i].substring(0, 1).toCharArray())[0];
        for (int j = i; j < strs.length; j++) {
            int indexJ = (int) (strs[j].substring(0, 1).toCharArray())[0];
            if (indexI > indexJ) {
                temp = strs[i];
                strs[i] = strs[j];
                strs[j] = temp;
            }
        }

    }
    for (int j = 0; j < strs.length; j++) {
        System.out.println(strs[j]);
    }
}
}

关于字符串的一些总结:

1.通过字符串函数 compareTo (string) ,compareToIgnoreCase(String) 及 compareTo(object string) 来比较两个字符串,并返回字符串中第一个字母ASCII的差值

public class StringCompareEmp{
public
static void main(String args[]){
String str = "Hello World";
String anotherString = "hello world";
Object objStr = str;
System.out.println( str.compareTo(anotherString) );
System.out.println( str.compareToIgnoreCase(anotherString) );
//
忽略大小写
System
.out.println( str.compareTo(objStr.toString()));
 
}
}
结果:-32   0    0
2.通过字符串函数 strOrig.lastIndexOf(Stringname) 来查找子字符串 Stringname 在 strOrig 出现的位置
public class SearchlastString {
public static void main ( String [ ] args ) {
String
strOrig = " Hello world ,Hello Runoob " ;
 
int lastIndex = strOrig . lastIndexOf ( " Hello " ) ;
 
if ( lastIndex == - 1 ) {
System . out . println ( " 没有找到字符串 Runoob " ) ;
}
else {
System . out . println ( " Runoob 字符串最后出现的位置: " + lastIndex ) ;
 
}
 
}
}
结果:
Runoob 字符串最后出现的位置: 13
3.字符串函数 substring() 函数来删除字符串中的一个字符

     
     
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 4.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
5.reverse() 将字符串反转:

        
        
public class StringReverseExample{ public static void main(String[] args){ String string="runoob"; String reverse = new StringBuffer(string).reverse().toString(); System.out.println("字符串反转前:"+string); System.out.println("字符串反转后:"+reverse); } } 结果:
字符串反转前:runoob
字符串反转后:boonur
6.String 类的 indexOf() 方法在字符串中查找子字符串出现的位置

public class SearchStringEmp {
public static void main ( String [ ] args ) {
String strOrig = " Google Runoob Taobao " ;
int intIndex = strOrig . indexOf ( " Runoob " ) ;
if ( intIndex == - 1 ) {
System . out . println ( " 没有找到字符串 Runoob " ) ;
} else {
System . out . println ( " Runoob 字符串位置 " + intIndex ) ;
}
}
}
结果:
Runoob 字符串位置 
7.split分割字符串
public static void main(String args[]){
String str = "www-runoob-com";
String[] temp;
String delimeter = "-";
// 指定分割字符
temp = str.split(delimeter);
//
分割字符串
// 普通 for 循环
for(int i =0; i < temp.length ; i++){
System.out.println(temp[i]);
System.out.println("");
}
}
结果:
www

runoob

com

8.String toUpperCase() 方法将字符串从小写转为大写
public class StringToUpperCaseEmp {
 
public static void main(String[] args) {
String str = "string runoob";
String strUpper = str.toUpperCase();
System.out.println("原始字符串: " + str);
System.out.println("转换为大写: " + strUpper);
}}
结果:
原始字符串: string runoob
转换为大写: STRING RUNOOB
9.regionMatches() 方法测试两个字符串区域是否相等

           
           
public class StringRegionMatch{ public static void main(String[] args){  String first_str = "Welcome to Microsoft"; String second_str = "I work with microsoft"; boolean match1 = first_str.regionMatches(11, second_str, 12, 9); boolean match2 = first_str.regionMatches(true, 11, second_str, 12, 9);  //第一个参数 true 表示忽略大小写区别 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
10."+" 操作符和StringBuffer.append() 方法来连接字符串
11.截取字符串
例如:截取指定的字符串
YPQD0006
String str = "房估字(2014)第YPQD0006号";
String jieguo = str.substring(str.indexOf("第")+1,str.indexOf("号"));
截取两个特定标记之间的字符

<abcd efg>higklmnopq<rstu vwxyz>ava代码如下:


public class StringTest {
public static void main(String[] args)
{
String str = "<abcd efg>higklmnopq<rstu vwxyz";
System.out.println(str.substring(str.indexOf(">")+1, str.lastIndexOf("<"))); }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值