【问题标题】:Get Number in Separated String of Numbers [duplicate]在分隔的数字字符串中获取数字[重复]
【发布时间】:2021-10-25 08:14:05
【问题描述】:

我有一个 JavaScript 字符串,它由两个由/ 分隔的数字组成。

我希望能够分别获得每个数字。

当只有1位整数时,我可以很容易地得到值,但是当有两位整数时,我不知道该怎么办。

个位数:

var string = "5/9";
var firstVal = string.slice(0, 1);
var secondVal = string.slice(-1);

多位数变化:

var string = "10/23";
var string = "9/21";
var string = "45/2";

【问题讨论】:

  • let [a, b] = '10/23'.split('/')?
  • 只需对每个字符串使用split('/') 函数。

标签: javascript


【解决方案1】:

const num = "23/2/3"
const result = num.split("/")

console.log(result);

您可以使用string.split 轻松实现这一目标。这称为字符串分隔符

【讨论】:

    【解决方案2】:

    你可以像这样使用split

    var string = "10/23";
    
    console.log(string.split("/"))

    【讨论】:

      【解决方案3】:

      尽管有运算符,您也可以匹配数字并省略不需要的部分。

      const
          string = ' 123/ 4 ',
          values = string.match(/\d+/g).map(Number);
      
      console.log(values);

      【讨论】:

        【解决方案4】:

        试着把你的问题分解成几个步骤。

        将字符串分成两部分

        console.log("bob/marie".split("/"));
        //  ["bob", "marie"]

        将字符串转换为数字

        假设您确定它们是有效数字。 在 Javascript 中有许多不同的方式来转换数字。我最喜欢的是乘以 1。

        console.log("132445.85" * 1);
        // 132445.85 as number

        结合两个操作

        console.log( "6532.11/4535.77".split("/").map( v => 1 * v ) );

        上面的地图在所有元素中都应用了这个函数。

        最后让我们把它放在一些 HTML 结果中的数据中,只是为了好玩

        function showNumbers(element) {
          // read the input value as string
          let text = element.value;
          // convert the string into an array breaking by "/"
          let numbers = text.split("/").map( v => v * 1 );
          // get the div element that we are going to put the list
          let myDiv = document.getElementById("my-div");
          // create a list
          let ul = document.createElement("ul");
          // creating a total column
          let total = 0;
          numbers.forEach(
            function(n) {
              // create a list item
              let li = document.createElement("li");
              // set the number as the list item value
              li.innerHTML = n;
              // add the list item into the list
              ul.appendChild(li);
              // we can add the numbers
              total += n;
            }
          )
          // create a list item
          let liTotal = document.createElement("li");
          // set the number as the list item value
          liTotal.innerHTML = "Total " + total;
          // add the list item into the list
          ul.appendChild(liTotal);
          // remove all elements from my div
          myDiv.innerHTML = "";
          // make the new list the element into the div
          myDiv.appendChild(ul);
        }
        <input type="text" style="width:200px" onKeyUp="showNumbers(this)"/>
        <div id="my-div">
        </div>

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-08-14
          • 1970-01-01
          • 1970-01-01
          • 2021-03-25
          • 1970-01-01
          • 2019-02-12
          • 2016-08-07
          相关资源
          最近更新 更多