简体   繁体   English

如何检查字符串中的每个数字?

[英]How do I check each individual digit within a string?

I was given the challenge of converting a string of digits into 'fake binary' on Codewars.com, and I am to convert each individual digit into a 0 or a 1, if the number is less than 5 it should become a 0, and if it's 5 or over it should become a 1. I know how to analyze the whole string's value like so: 在Codewars.com上,我面临将数字字符串转换为“伪二进制”的挑战,并且我要将每个数字转换为0或1,如果数字小于5,则应变为0,并且如果大于等于5,则应该变为1。我知道如何像这样分析整个字符串的值:

function fakeBin(x){
if (x < 5)
return 0;
else return 1;
}

This however, analyzes the value of the whole string, how would I go about analyzing each individual digit within the string rather than the whole thing? 但是,这将分析整个字符串的值,我将如何分析字符串中的每个数字而不是整个数字?

Note: I have already looked at the solutions on the website and don't understand them, I'm not cheating. 注意:我已经查看了网站上的解决方案,但不理解它们,我不是在作弊。

You can do it in one line with two simple global string replacement operations: 您可以通过两个简单的全局字符串替换操作在一行中完成此操作:

 function fakeBin(x){ return ("" + x).replace(/[0-4]/g,'0').replace(/[5-9]/g,'1'); } console.log(fakeBin(1259)) console.log(fakeBin(7815)) console.log(fakeBin("1234567890")) 

The ("" + x) part is just to ensure you have a string to work with, so the function can take numbers or strings as input (as in my example calls above). ("" + x)部分只是为了确保您可以使用一个字符串,因此该函数可以将数字或字符串作为输入(如上述示例调用中所示)。

Simple javascript solution to achieve expected solution 简单的javascript解决方案以实现预期的解决方案

function fakeBin(x){
 x = x+'' ;
var z =[];
for(var i=0;i< x.length;i++){
  if((x[i]*1)<5){
     z[i] =0;
   }else{
    z[i]=1;
  }
}
  return z
}

console.log(fakeBin(357))

Split the string and apply the current function you have to each element of the string. 拆分字符串,然后将当前函数应用于字符串的每个元素。 You can accomplish this with map or with reduce : 您可以使用mapreduce来实现:

 function fakeBin(x) { x = x.split(''); let toBin = x => { if (x < 5) return 0; else return 1 } return x.map(toBin).join(''); } console.log(fakeBin("2351")); 

refactored 重构的

 function fakeBin(x) { x = [...x]; let toBin = x => x < 5 ? 0 : 1; return x.map(toBin).join(''); } console.log(fakeBin("2351")); 

reduce 降低

 function fakeBin(x) { let toBin = x => x < 5 ? 0 : 1; return [...x].reduce((acc,val) => acc + toBin(val), ""); } console.log(fakeBin("23519")); 

if you are in java you can use 如果您使用的是Java,则可以使用

charAt()

and you make a for with the word length and you can check one by one 然后用字长做for,就可以一一检查

for(int i = 0; i < text.length(); i++){
yourfunction(texto.charAt(i));
}

The snippet below will take a string and return a new string comprised of zeros and/or ones based on what you described. 以下代码段将采用一个字符串,并根据您的描述返回一个新的字符串,该字符串包括零和/或一。

We use a for ...of loop to traverse the input string and will add a 0 or 1 to our return array based on whether the parsed int if greater or less than 5. 我们使用for ...of循环遍历输入字符串,并将根据解析的int是大于还是小于5将0或1添加到返回数组中。

Also note that we are checking and throwing an error if the character is not a number. 另请注意,如果字符不是数字,我们正在检查并抛出错误。

 const word = "1639"; const stringToBinary = function(str) { let ret = []; for (const char of word) { if (Number.isNaN(parseInt(char, 10))) { throw new Error(`${char} is not a digit!`); } else { const intVal = parseInt(char, 10); ret.push(intVal > 5 ? 1 : 0); } } return ret.join(''); }; console.log(stringToBinary(word)); 

You can use String.prototype.replace() with RegExp /([0-4])|([5-9])/g to match 0-4 , 5-9 , replace with 0 , 1 respectively 可以使用String.prototype.replace()RegExp /([0-4])|([5-9])/g ,以匹配0-45-9 ,取代以01分别

 let str = "8539734222673566"; let res = str.replace(/([0-4])|([5-9])/g, (_, p1, p2) => p1 ? 0 : 1); console.log(res); 

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

相关问题 如何检查我的字符串是否以 WA 后跟 9 位数字结尾 - How do I check if my string ends with WA followed by 9 digit 如何检查 3 个变量中的每一个是否都是空字符串? - How do I check if each of 3 variables are the empty string? 如何替换字符串中的一位数字而又不影响该字符串中的2位数字 - How can I replace single digit numbers within a string without affecting 2 digit numbers in that string 如何检查表单域上按下的键是否为数字 (0 - 9)? - How do I check if the key pressed on a form field is a digit (0 - 9)? 如何为每个div分配随机的背景颜色? - How do I give each individual div a random background color? 如何在字符串中的字符串中传递字符串? - How do I pass a string within a string within a string? 如何在JavaScript中更改字符串中的各个字符 - How to change individual characters within a string in JavaScript Angular.js-如何使用过滤器检查数组对象中是否包含字符串? - Angular.js - How do I check to see if a string is contained within an array object using a filter? 如何检查用户提交的文本输入中的特定字符串? - How do I check for specific string within text input submitted by a user? 如何获得包含值的字符串数组中每个字符串名称的最大值? - How do you get the highest value of each individual string name in an array of strings that contain a value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM