2012-05-09 57 views
0

如何從偶數/奇數狀態中刪除最後一個|如何從保留字符列表中刪除最後一個字符?

實施例:

import java.io.*; 
import java.net.Socket; 

public class Test { 

    public static String _noManualTraceIt = "0008e07cd8ec6cfdb900283508004500004a7d8d400080060000c0a80087c0a800c6c6931388956304704eebd50c5018ffff82da0000674500000504000000000000030000000000000000000000060000000000ff210000"; 
    public static void main(String[] args) throws IOException { 
    System.out.println(wireShirk(_noManualTraceIt)); 
    } 

    public static byte[] wireShirk(String bytes) throws IOException { 
    String temp = ""; 
    String replaceIfOnLastFound = "||&|\r\n|\n\r|etc|etc"; 

    int _i = 0; 
    do { 
     temp += bytes.charAt(_i);  
     if(_i%2 ==0) { 
     } else { 
     temp += "|"; 
     } 
     System.out.println(bytes.length() + " " + _i + " " + two.charAt(_i) + " " + temp); 
     _i++; 
    } while(_i<bytes.length()); 

    bytes = temp; // << replaceIfOnLastFound any of those characters 
        // | or & or \r\n or etc etc from that variable 


    String byteArrayStr[] = bytes.split("\\|"); 
    byte bytesArray[] = new byte[byteArrayStr.length]; 
    for (int i = 0; i < byteArrayStr.length; ++i) { 
     bytesArray[i] = (byte) (Integer.parseInt(byteArrayStr[i], 16)); 
    } 
    return bytesArray; 
    } 

} 

輸出:

176 175 0 00|08|e0|7c|d8|ec|6c|fd|b9|00|28|35|08|00|45|00|00|4a|7d|8d|40|00|80|06|00|00|c0|a8|00|87|c0|a8|00|c6|c6|93|13|88|95|63|04|70|4e|eb|d5|0c|50|18|ff|ff|82|da|00|00|67|45|00|00|05|04|00|00|00|00|00|00|03|00|00|00|00|00|00|00|00|00|00|00|06|00|00|00|00|00|ff|21|00|00| 
+0

您如何期望從字符串中刪除最後一個字符?這並不難。 –

+1

http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#substring%28int,%20int%29 – phineas

回答

3

添加如果內部循環語句..

} else { 
    if(i < bytes.length - 1){ 
    temp += "|"; 
    } 
    } 
0

下面是一個簡單的例子,在不使用任何邏輯,但有一個常數(重新)賦值分隔符變量:

String separator = ""; 
String output = ""; 
for (String s : my_list) { 
    output += separator + s; 
    separator = "|"; 
} 
1

重新排序將管道符號的步驟和十六進制字符,並添加條件

if (temp.length() > 0) { 
    temp += '|'; 
} 

這樣,你先添加分隔符,而不是爲第一個循環迭代。

相關問題