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

时间:2010-01-11 15:08:21

标签: javascript string arrays

CSS:

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

使用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:

<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>

4 个答案:

答案 0 :(得分:10)

您可以使用join加入数组项:

strMessage = myNames.join(", ")

甚至更好:

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

答案 1 :(得分:4)

你可以用“。”替换最后一个“,”。由:

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

答案 2 :(得分:2)

为什么不把它放在首位?

即,在这个区块中:

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

检查我们是否在列表的末尾,并在逗号的位置放置一段时间:

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

答案 3 :(得分:0)

假设你的意思是逗号,不常见,String.replace(",[^,]+$",".")会有效吗?

请注意,如果您阅读了良好的文档,则可以在大多数语言中找到相同类型的功能。