简体   繁体   English

Javascript将字符串拆分为不同大小的子字符串

[英]Javascript split string in different sized substrings

I need to code an algo that, giving a hex string,generates an array with 5 elements of 4 hex digits and another array with 2 elemetents of 2 hex digits long. 我需要编写一个算法,给出一个十六进制字符串,生成一个包含5个4个十六进制数字元素的数组,以及另一个包含2个2个十六进制数字元素的数组。 The unique two premises are: 唯一的两个前提是:

  • if the 4 digit string is = "0000" don't take it into account and if the 2 digit long hex is "00" (EDIT: or "01") don't take it into account neither. 如果4位数字字符串=“ 0000”,则不要考虑;如果2位数字长十六进制是“ 00”(EDIT:或“ 01”),也不要考虑。
  • don't use the digits used for the five 4char strings for the 2char strings. 不要将用于5个4char字符串的数字用于2char字符串。 I don't mind if one starts form the beginning and the other one from the end or the middle, or both from the middle...i mean the substrings don't need to be consecutive, they just have to be different from 0 and same string positions not used in both arrays. 我不介意一个是从开头开始,另一个是从结尾开始还是从中间开始,或者两者都从中间开始……我的意思是子字符串不需要是连续的,它们只需要与0不同即可和两个数组中未使用的相同字符串位置。 Edit: Imagine that the five 4char sequences are built using the first 20 characters (there is no "0000" sequence after splitting the main string every four chars). 编辑:假设使用前20个字符构建了五个4char序列(将主字符串每四个char分割后就没有“ 0000”序列)。 That means that the two 2char sequences don't have to take into account the first 20 chars as they have already been used. 这意味着两个2char序列不必考虑前20个char,因为它们已经被使用过。

Example of one correct output: 一个正确输出的示例:

String : 87b86156d0000a4200005e02002f56614f7a2f54f7ebf45670ed62cbaa78e6f228297b0e7338215fb4 字符串:87b86156d0000a4200005e02002f56614f7a2f54f7ebf45670ed62cbaa78e6f228297b0e7338215fb4

first array: ["87b8", "6156", "d000", "0a42", "5e02"] 第一个数组:[“ 87b8”,“ 6156”,“ d000”,“ 0a42”,“ 5e02”]

second array: ["2f", "56"] 第二个数组:[“ 2f”,“ 56”]

My approach starts like this: 我的方法是这样开始的:

var mystring = "87b86156d0970a4200005e02612f56614f7a2f54f7ebf45670ed62cbaa78e6f228297b0e7338215fb4"
var firststring = mystring.match(/.{1,4}/g);

Which gives me: 这给了我:

["87b8", "6156", "d097", "0a42", "0000", ...] [“ 87b8”,“ 6156”,“ d097”,“ 0a42”,“ 0000”,...]

then: 然后:

for (x=0;x<firststring.length;x++){
        if (firststring[x]=="0000") {firststring.splice(x, 1)}
}

  var secondstring = mystring.match(/.{1,2}/g);

Which gives me: 这给了我:

["87", "b8", "61", "56", "d0", "97"...] [“ 87”,“ b8”,“ 61”,“ 56”,“ d0”,“ 97” ...]

for (x=0;x<secondstring.length;x++){
        if (secondstring[x]=="00") {secondstring.splice(x, 1)}
}

Here is where i am lost, i don't know exactly how to code the part where i avoid the main string digits (positions) to be used in both arrays... 这是我迷路的地方,我不知道该如何编码避免在两个数组中使用主字符串数字(位置)的部分。

Regards, 问候,

Well, it's not pretty, but it works: 好吧,它并不漂亮,但是可以工作:

var str = "87b86156d0970a4200005e02612f56614f7a2f54f7ebf45670ed62cbaa78e6f228297b0e7338215fb4";

var grabLength = 4,
    // 4-char strings
    grp1 = [],
    // 2-char strings
    grp2 = [],
    chunk;

while(chunk = str.slice(0, grabLength)) {
  str = str.slice(grabLength);

  // skip all zeros
  if (/^0+$/.test(chunk)) continue;

  if (grabLength === 4) {
    if (grp1.push(chunk) === 5) {
      grabLength = 2;
    }
    continue;
  }

  // skip 2-char sequences that match the start of a 4-char sequence
  var hasMatch = false;
  for (var i = 0; i < grp1.length; i++) {
    if (chunk === grp1[i].slice(0, 2)) {
      hasMatch = true;
      continue;
    }
  }
  if (hasMatch) continue;

  grp2.push(chunk);

  if (grp2.length === 2) break;
}

console.log(grp1);
console.log(grp2);

JSBin JSBin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM