How can I make new line or break in array?
Adding a newline will not work, as HTML will ignore any whitespace/newlines to best format string as contiguous as possible. So you have to use <br/>
to add an explicit line-break in HTML. However, now, in rails for safety any string will be html-escaped unless you explicitly instruct rails to not do that.
So in your case you want to build the list of files, claim it is safe, and then set as content. Something like
- file_list = folder.doc.map{|file_id| File.find(file_id).name}.join('<br/>').html_safe
%i.fa.fa-folder.item{data: {content: file_list, html: "true", rel: "popover", title: "title:"}
Secondly, because you are using bootstrap to render the popover, you will also have to instruct the bootstrap popover that the data-content
is interpreted as html and should not be escaped (by setting the data-html
attribute).
Break Array Values Into New Line in Angular
The problem is that error.error.error
in an array and you are printing it as it is.
Try this
var error = {"headers": { "normalizedNames": {}, "lazyUpdate": null},"status": 404,"statusText": "Not Found","url": "http://sample.com","ok": false,"name": "HttpErrorResponse","message": "Http failure response for http://sample.com: 404 Not Found","error": { "error": [ "Error1", "Error2", "Error3" ]}}var alertString = 'error ';for (var i = 0; i < error.error.error.length; i++) {alertString = alertString + '\n ' + error.error.error[i];}alert(alertString); console.log(alertString);
add a new element to an array as a line break
Use double quotes "\n"
instead of '\n'
single quotes, Single quotes treats as string
<?php
$arr = [1,2,3,"\n","after line break"];
echo implode('',$arr);
?>
Live demo : https://eval.in/865569
In your browser to add line break use <br>
instead \n
PHP Line break in array
You have two problems:
$data
should be a string not an array.
concatenates the left hand side and the right hand side:"abc" . "def"
becomes"abcdef"
.- putting the dot first like in
. "\n"
or even. "\n" $mail
doesn't make sense in PHP so you'll get a parse error.
- putting the dot first like in
Replace your $data =
line with $data = $name . "\n" . $mail . "\n" . $saywords;
and you'll be good to go.
split string value but keep the line break inside new array
You need this
let bodyText = "Test
Newline here"
let bodyTextArray = bodyText.split(/( |\n)/)
If you put capturing ()
around what you are splitting on then that is added to the output
Split string by new line characters
You can use the explode
function, using "\n
" as separator:
$your_array = explode("\n", $your_string_from_db);
For instance, if you have this piece of code:
$str = "My text1\nMy text2\nMy text3";
$arr = explode("\n", $str);
var_dump($arr);
You'd get this output:
array
0 => string 'My text1' (length=8)
1 => string 'My text2' (length=8)
2 => string 'My text3' (length=8)
Note that you have to use a double-quoted string, so \n
is actually interpreted as a line-break.
(See that manual page for more details.)
How to break lines in javascript using .join
You can use a for
loop with Array#slice
.
const arr = ["a", "b", "c", "d", "e", "f", "g", "h", "i"];
const parts = [];
for(let i = 0; i < arr.length; i += 3){
parts.push(arr.slice(i, i + 3).join(' '));
}
console.log(parts.join('\n'));
Convert text file separated with line breaks to an array in JavaScript
Your text is separated with line breaks \n
. You can split into an array using split("/n")
. In the event that there are spaces in your text, you should map
that array through a trim()
filter to remove the spaces.
let pre = document.querySelector('pre').innerHTML;
//without trimming
let arr = pre.split("\n");
console.log(arr)
//with trim
let trimmed = pre.split("\n").map(e=>e.trim());
console.log(trimmed)
<pre>
banana
apple
orange
lemon
grape
</pre>
Related Topics
How to Detect Browser Refresh Using Angular
Regular Expression to Get a String Between Two Strings in JavaScript
Retaining the Textbox Values Even After Refresh
Regex to Find Json Values Without Quotes
Check If a String Has White Space
How Add a New Key/Value Pair to a Json Object
How to Force Clients to Refresh JavaScript Files
Javascript/Regex: Remove Text Between Parentheses
How to Concatenate String and Numbers to Make a Json Object
Detect If User Is Using Webview for Android/Ios or a Regular Browser
Regex for Names Validation Allow Only Letters and Spaces
Javascript: Replace Last Occurrence of Text in a String
Add Two Numbers and Display Result in Textbox With JavaScript
How to Change Colour of Text Based on Its Value When a Page Initially Loads
How to Remove Special Character from Json Without Parsing
React Native Dynamically Change View's Background Color
Securityerror: Blocked a Frame With Origin from Accessing a Cross-Origin Frame