简体   繁体   English

Javascript:如何将两个Stringarray-元素添加到一个String中且不折行

[英]Javascript: How to add two Stringarray - Elements to one String and without breaking lines

I want to print randomly a name with first name and surname. 我想随机打印一个名字和姓氏。 But between the first name and surname the lines were breaking. 但是名字和姓氏之间的界线断了。

The Code: 编码:

            var req1 = read("germannames.txt");
            var req2 = read("surnames.txt");
            var firstNames = req1.split(";");
            var surNames = req2.split(";");
            var randoms = "numbers:";

            var times= parseInt(document.getElementById('times').value);

         for (var i = 0; i < times; i++) {

                var firstNumber = parseInt(Math.random() * firstNames.length +1);
                var surNumber = parseInt(Math.random() * surNames.length +1);

                var name = firstNames[firstNumber] + surNames[surNumber];

                document.getElementById('result').value += name;
        }

I read the text for the array: 我阅读了数组的文本:

        function read(fileName)
        {
            var xhr = new XMLHttpRequest();

            xhr.open("GET", "http://localhost/Bowixel/" + fileName, false);

            xhr.send(null);

            return xhr.responseText;
        }

The output: 输出: 输出量

And how it should work: 以及它应该如何工作: 结果

I don't know how to solve this problem. 我不知道如何解决这个问题。

You may need to remove the line feeding from the firstNames[firstNumber] , such as 您可能需要从firstNames[firstNumber]删除换行符,例如

var name = firstNames[firstNumber].replace(/^\n|\n$/g, '') + surNames[surNumber];

or try to use trim() if it is not a problem to remove the whitespace from both sides of a firstNames, like 或尝试使用trim()如果从firstNames的两侧删除空格不是问题,例如

var name = firstNames[firstNumber].trim()  + ' '+ surNames[surNumber];

暂无
暂无

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

相关问题 如何在不破坏javascript代码的情况下用javascript字符串换行? - how to give a line break with the javascript string without breaking javascript code? 在 javascript 代码中将字符串分成多行 - Breaking a string into multiple lines inside a javascript code 如何在不破坏多字节字符的情况下截断 JavaScript 中的 UTF8 字符串? - How to truncate UTF8 string in JavaScript without breaking multibyte characters? 如何在javascript中剪切字符串,使其恰好适合两行并在最后添加.... - How to cut the string in javascript so that it fits exactly two lines & add … at the end 在 Javascript 中分块/拆分字符串而不破坏单词 - chunk/split a string in Javascript without breaking words 将字符串分成不同的行? - breaking a string into different lines? 如果我的javascript数组的值包含两个元素,如何使用一个元素而不使用另一个元素? - If my javascript array has values that have two elements how can I use one element without the other? 如何在javascript中合并两个字符串数组的元素? - How to merge elements of two string arrays in javascript? 如何在控制台中将文本添加到值,而不破坏它 - How to add text to value in console, without breaking it Javascript正则表达式:如何检测url是否为http://形式 <any string without a dot> .one.two.com? - Javascript Regex: How to detect if a url is of the form http://<any string without a dot>.one.two.com?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM