zoukankan      html  css  js  c++  java
  • 判断字符串只能有且只有一处连续数字不同

    字符串中只能有且只有一处连续的数字不同,非数字必须相同(前面有不同数字,下标可以不同,否则下标必须相同。),

    A001B001 与A002B001  OK

    A001B001 与A002B002  NO 两处不同

    A001B001 与A02B001  OK   001与02长度可以不同

    001           与002           OK

    B01          与B0000002    OK

    B001B01 与B002A01    NO   B与A是非数字,必须相同

    B001B01 与B2B01        OK   B与B的下标可以不同,因为前面有不同的数字,

    B001B01 与B001A02    NO   B与A是非数字,因为前面没有不同的数字,下标必须相同,

    public class VersionName {
    
    	public static void main(String[] args) {
    		String[] str1 = { "B001", "B001N001", "B001N001","B001N001", "A001BBB001", "A002B",   "A001BBB","A001B000","A001BBB","A001B0002","001AA001","AAA" ,"0001","000","AAA","", ""};
    		String[] str2 = { "B002", "B002N001", "B01N001", "B01N002",  "A002BBB002", "A001BBB", "A001B",  "A001B000004","A001BBB", "A001B01",  "01AA001", "AAA" ,"0002","000","AAB","1",""};
    		VersionName version = new VersionName();
    		for (int i = 0; i < str1.length; ++i) {
    			version.getDifferent(str1[i], str2[i]);
    		}
    	}
    
    	public String getDifferent(String versionA, String versionB) {
    		System.out.print(versionA + "与" + versionB+":");
    		boolean isDifferent = false;
    		int beginA = -1, beginB = -1, lengthA = versionA.length(), lengthB = versionB.length();
    		String numberA = "", numberB = "";
    		int i = 0, j = 0, index = 0;
    		for (; i < lengthA && j < lengthB;) {
    			char ch = versionA.charAt(i);
    			if (Character.isLetter(ch)) {
    				if (ch == versionB.charAt(j)) {
    					++i;
    					++j;
    				} else {
    					System.out.println(ch + "字母不同" + versionB.charAt(j)+false);
    					return null;
    				}
    			} else {
    				beginA = i;
    				beginB = j;
    				while ((++i) < lengthA && Character.isDigit(versionA.charAt(i))) {
    
    				}
    				while ((++j) < lengthB && Character.isDigit(versionB.charAt(j))) {
    
    				}
    				if (i >= lengthA) {
    					i = lengthA;
    				}
    				if (j >= lengthB) {
    					j = lengthB;
    				}
    				String numberTemA = versionA.substring(beginA, i);
    				String numberTemB = versionB.substring(beginB, j);
    				if (numberTemA.equals(numberTemB)) {
    
    				} else {
    					if (isDifferent) {
    						System.out.println("不止一处不同" + numberA + "," + numberB + "," + numberTemA + "," + numberTemB+false);
    						return null;
    					} else {
    						isDifferent = true;
    						index = beginA;
    						numberA = numberTemA;
    						numberB = numberTemB;
    					}
    				}
    			}
    		}
    		if (isDifferent && i >= versionA.length() && j >= versionB.length()) {
    			String tem = index + ":" + numberA + ":" + numberB;
    			System.out.println(" " + tem + " " + true);
    			return tem;
    		} else {
    			System.out.println(" " + false);
    			return null;
    		}
    	}
    }
    

      

  • 相关阅读:
    UML用例图
    directX--大约CSource和CSourceStream (谁在叫fillbuffer)
    【云】如何建立微信在全国卖场地图?
    C++ Primer 学习笔记_38_STL实践与分析(12)--集成的应用程序容器:文本查询程序
    Android使用SVG矢量创建很酷的动态效率!
    观察者模式(observer行为)c#简单的例子
    Oracle MySQL
    tomcat 跨域
    Jms Spring
    Ehcache使用
  • 原文地址:https://www.cnblogs.com/duange/p/8825108.html
Copyright © 2011-2022 走看看