简体   繁体   English

如何从字符串中删除最后一个逗号并将其替换为Javascript中的句点?

[英]How do I remove the last comma from my string and replace it with a period in Javascript?

CSS: CSS:

    .dynamicDiv {
    width:200px;
    border:solid 1px #c0c0c0;
    background-color:#e1e1e1;
    font-size:11px;
    font-family:verdana;
    color:#000;
    padding:5px;
    }

Javascript: Javascript:

    //create array
    var myNames=new Array();
    //var nameString=document.getElementById(myNames.toString())

    function addToDiv()
    {
        //1) add name to div
        document.getElementById('divName').innerHTML = document.getElementById('divName').innerHTML + document.getElementById('enter_name').value + "<br>";
        //2) now add the value to an array
        myNames.push(document.getElementById('enter_name').value);
        //strHTML = document.getElementById('divName').innerHTML + document.getElementById('enter_name').value + "<br>";
        //document.getElementById('divName').innerHTML = strHTML;
    }

    function displayMessage()
    {
        //debugger;
        var strMessage = "Welcome to this page ";

        //loop throught the array and add the element values to strMessage
        for(i=0;i<=myNames.length-1;i++)
        {
            strMessage = strMessage + myNames[i] + ", "
        }

        //remove trainling comma
        //strMessage = strMessage

        alert(strMessage);
    }

    function displayMessagebye()
    {
        //debugger;
        var strMessage = "Thank you for coming to this page ";

        //loop throught the array and add the element values to strMessage
        for(i=0;i<=myNames.length-1;i++)
        {
            strMessage = strMessage + myNames[i] + ", "
        }

        //remove trainling comma
        //strMessage = strMes
        //strMessage = strMessage.substr(
        alert(strMessage);
    }

HTML : HTML:

<div class="dynamicDiv">
  <input type="text" name="enter_name" id="enter_name" />
  <input name="button" id="button" value="Submit" type="button" onclick="addToDiv();"/>
</div>

<div id="divName" class="dynamicDiv" style="margin:10px 0px;">
<h2>Names List</h2><p>Names of person you've already added.</p>
</div>
<div class="dynamicDiv"><input type="button" onclick="displayMessagebye();" value="Loop" /></div>
<div id="loop" > </div>
<div class="dynamicDiv"><input type="button" onclick="displayMessage();" value="String" /></div>
<div id="string" > </div>

You can use join to join the array items: 您可以使用join来加入数组项:

strMessage = myNames.join(", ")

Or even nicer: 甚至更好:

if (myNames.length <= 1) {
    strMessage = myNames.join();
} else {
    strMessage = myNames.slice(0, -1).join(", ") + " and " + myNames[myNames.length-1];
}

You could replace the last ", " with "." 您可以将最后一个“,”替换为“”。 by: 通过:

strMessage.replace(/, $/, ".")

Why not just not put it there in the first place? 为什么不把它放在第一位呢?

ie, in this block: 即,在此块中:

for(i=0;i<=myNames.length-1;i++)
{
    strMessage = strMessage + myNames[i] + ", "
}

Just check whether we're at the end of the list and put a period there inb place of the comma: 只需检查我们是否在列表的末尾,然后在逗号的开头放置一个句点即可:

for(i=0;i<=myNames.length-1;i++)
{
    strMessage = strMessage + myNames[i] 
    if(i < myNames.length)
       strMessage += ",";
    else
       strMessage = ".";  
}

Assuming you meant comma, not common, will String.replace(",[^,]+$",".") work? 假设您的意思不是逗号,那么String.replace(",[^,]+$",".")工作吗?

Note that you can find this same type of functionality in most languages, if you read good documentation. 请注意,如果您阅读了不错的文档,则可以在大多数语言中找到相同类型的功能。

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

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