切割的方法和代码我不会用,有偿求程序演示
不带X的数据切割规则就是,连续的1遇0结束,连续的0遇1结束
例如101110 切割后就是10 1110
带X的数据是X出现在切割首位,则X取值为X后面的数字
上图X就取值0
X出现的切割中间,则X取X前的数字
上图X就是1后的X取1,0后的X取0
取值后就可以进行上述不带X切割的步骤
这是我的思路,但是切割这块我搞不明白怎么弄
供参考
import com.sun.deploy.security.SelectableSecurityManager;
public class Main {
/**
* 在源数据中将指定字符替换成最后的数字
* @param sourceData
* @param repalceChar
* @param index
* @return
*/
public static String replaceDataFromTail(String sourceData, String repalceChar,int index) {
if (!sourceData.substring(index, index + 1).equals(repalceChar))
return sourceData.replaceFirst(repalceChar, sourceData.substring(index, index+1));
if (sourceData.substring(index, index + 1).equals(repalceChar)&& (index== sourceData.length()-1))
return sourceData.replaceFirst(repalceChar, sourceData.substring(index, index+1));
return replaceDataFromTail(sourceData, repalceChar,index+1);
}
/**
* 在源数据中用前面的数字替换指定字符
* @param sourceData
* @param repalceChar
* @param index
* @return
*/
public static String replaceDataFromHead(String sourceData, String repalceChar,int index) {
if (index> sourceData.length()-1)
return sourceData;
if (!sourceData.substring(index,index+1).equals(repalceChar))
return sourceData;
String prevOneChar= "";
String prevTwoChar= "";
String prevThreeChar= "";
switch (index)
{
case 0:
break;
case 1:
prevOneChar = sourceData.substring(index - 1, index);
sourceData = sourceData.replaceFirst(repalceChar, prevOneChar);
sourceData = replaceDataFromHead(sourceData, repalceChar, index + 1);
break;
case 2:
prevOneChar = sourceData.substring(index - 1, index);
prevTwoChar= sourceData.substring(index-2, index-1);
if(prevOneChar.equals(prevTwoChar)) {
sourceData = sourceData.replaceFirst(repalceChar, prevOneChar);
sourceData = replaceDataFromHead(sourceData, repalceChar, index + 1);
}
else
{
return replaceDataFromTail(sourceData,repalceChar,index);
}
break;
default:{
prevOneChar = sourceData.substring(index - 1, index);
prevTwoChar= sourceData.substring(index-2, index-1);
prevThreeChar= sourceData.substring(index-3, index-2);
if ((!prevThreeChar.equals(prevTwoChar)|| prevTwoChar.equals(prevOneChar))&&(!prevOneChar.equals(repalceChar)))
{
sourceData= sourceData.replaceFirst(repalceChar, prevOneChar);
sourceData= replaceDataFromHead(sourceData,repalceChar,index+1);
}
else
{
return replaceDataFromTail(sourceData,repalceChar,index);
}
}
}
return sourceData;
}
/**
* 根据规则拆分指定字符串
* @param sourceData
* @return
*/
public static String splitData(String sourceData, String repalceChar)
{
String tmpData= "";
int startIndex= -1;
for (int index=0;index< sourceData.length();index++){
if (sourceData.substring(index,index+1).equals(repalceChar)) {
sourceData= replaceDataFromHead(sourceData,repalceChar,index);
}
if (startIndex==-1) {
startIndex = index;
continue;
}
if (!sourceData.substring(index,index+1).equals(sourceData.substring(index-1,index)))
{
tmpData= tmpData+" "+ sourceData.substring(startIndex,index+1);
startIndex= -1;
}
}
if (startIndex!= -1)
{
tmpData= tmpData+" "+ sourceData.substring(startIndex,sourceData.length());
}
return tmpData;
}
public static void main(String[] args){
String sourceInfo= "111101XXX011XXXX0XX";
System.out.println("源数据:"+ sourceInfo);
String replaceInfo= splitData(sourceInfo,"X");
System.out.println("替换后数据:"+ replaceInfo);
}
}