This question already has answers here:
split string into array of n words per index
(5 answers)
Closed 6 years ago.
I have the following string:
The water content is considered acceptable for this voltage class. Dielectric Breakdown Voltage is unacceptable for transformers > 288 KV. Power factors, Interfacial Tension and Neutralization Number are acceptable for continued use in-service.".
I want to split the string into lines so that every line will contain at max 5 words in each line.
I want to control the number of words in each line dynamically, so that tomorrow I will be able to split the string into lines where each line contain at max N sentences in each line.
var string="The water content is considered acceptable for this voltage class. Dielectric Breakdown Voltage is unacceptable for transformers > 288 KV. Power factors, Interfacial Tension and Neutralization Number are acceptable for continued use in-service.";
var yourSplit=function(N,string){
var app=string.split(' '),
arrayApp=[],
stringApp="";
app.forEach(function(sentence,index){
stringApp+=sentence+' ';
if((index+1)%N===0){
arrayApp.push(stringApp);
stringApp='';
}else if(app.length===index+1 && stringApp!==''){
arrayApp.push(stringApp);
stringApp='';
}
});
return arrayApp;
};
console.log(yourSplit(5,string));
console.log(yourSplit(3,string));
console.log(yourSplit(8,string));
Related
This question already has answers here:
Split string at space after certain number of characters in Javascript
(5 answers)
Split large string in n-size chunks in JavaScript
(23 answers)
Closed 5 months ago.
MESSAGE FOR MOD: This question is about keeping full sentences, the linked questions only consider words.
Take the following example:
// I have a long string that I split into sentences
const input =
"This is the first sentence... This is the second, much longer sentence, with some additional puntuations?! Third sentence with a different length! Just a sentence ending with a number 980. Last but not least, the fourth sentence.";
// I used the following code to split the long string into sentences:
const arr = input.replace(/([.?!])\s*(?=[a-zA-Z0-9])/g, "$1|").split("|");
// we can assume that a sentence from the input array does not exceed this limit
const maxLength = 105;
// TODO: magic happens
/* I'm trying to get an array with the sentences re-joined
by a space, split so that one string does not exceed the limit
[
"This is the first sentence... This is the second, much longer sentence, with some additional puntuations?!",
"Third sentence with a different length! Just a sentence ending with a number 980.",
"Last but not least, the fourth sentence."
]*/
codesandbox: https://codesandbox.io/s/string-array-split-max-chunk-length-bfteuh?file=/index.ts
Im trying to get an array that combines the strings of the initial array with a space but considers that the resulting strings cannot exceed a maximum length. Also the strings have to contain full sentences (you can assume a single sentence won't exceed the limit). Also consider "...", "?!", "???", etc. as possibile sentence endings in the original input string.
How would you go about this? Do I have to use recursion to get some kind of concise code? Is recursion the most elegant solution?
Note: So far I tried a reducer but thought that I would have to use the rest of the array in a recursive function.
This question already has answers here:
Split String By Multiple Spaces NodeJS
(2 answers)
Closed 5 years ago.
The output from ps can give you lines like this one:
0.0 0.2 88 /usr/sbin/securityd
Or like this one:
47.0 0.3 7770 node
Or even:
1.0 2.5 585 /Applications/PhpStorm.app/Contents/MacOS/phpstorm
The number of white spaces between columns is variable according to the content of the columns. If you want to extract the columns values I did an ugly solution:
line.replace(' ', ' ').replace(' ', ' ').replace(' ', ' ').split(' ');
That covers everything from 4 spaces to 2. If I want to support 5 spaces I'll need another replace, and to be honest I don't like this solution. I thought about parsing each char at the time looking for spaces and accumulating consecutive nonspaces chars, but I wonder if there is a better way, maybe with regular expressions?
You can use regular expressions with the split function to match any number of spaces.
var lines = line.split(/\s+/);
The \s+ regular expression matches one or more spaces.
A runnable example with the strings you provided:
var lines = [
"0.0 0.2 88 /usr/sbin/securityd",
"47.0 0.3 7770 node",
"1.0 2.5 585 /Applications/PhpStorm.app/Contents/MacOS/phpstorm"
];
for (var i in lines)
console.log(lines[i].split(/\s+/));
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
If I say 2 bytes can hold 510 characters of data will I be wrong? based on the fact that 1 byte can hold 0-255 max character
One byte is :
8 bits, each one can be either 0 or 1
something that can represent 256 distinct values
Two bytes are ..two bytes.
16 bits
something that can represent 65536 distinct values
There is no meaning in what a byte (or two bytes) is, if you don't know the encoding used, what each single one of the 256 (or 65536) values are supposed to be/mean.
If you're talking about Char, you can't either say it's one or two or fifty chars...
ASCII encoding holds 128 distinct characters (95 chars can be displayed while the remaining are control chars) ranging from code 0 to 127 (Byte value expressed in decimal literal)
Unicode encoding (v7) is a generic encoding. You have the UTF-8, the UTF-16 Little Endian or Big Endian, and the UTF-32 Little Endian or Big Endian.
UTF-8 requires either 1, 2, 3 or 4 bytes to represent one single character.
UTF-16 is a fixed-size character encoding : each char requires 2 bytes.
UTF-32 is also a fixed size character encoding that requires 4 bytes per character.
There are hundreds of different Encodings that can represent one character for each of the 256 unique values a single Byte can represent. Like ANSI.
So I tend to say, yes, you're wrong thinking two bytes can hold 510 characters of data, assuming you're using one of the above encoding or similar.
But again, a Byte is a Byte, not a Char !
Let's imagine a (new) custom encoding with specific parser and formatter where each bit [0 or 1] define the selection of one word/text/string stored in a dictionary, and following words/text/string selection depends on the previous selected word (previous bit value)
The purpose of such type of encoding is somewhat useless, but hey ! Because you used a dictionary, you can affirm one single byte can represent exactly 510 characters of data (or even more) because of the use of this specific encoding/decoding..!
Again, a byte is a byte, saying it holds one, two, zero or 510 characters doesn't mean anything if you don't define first what is the encoding used.
EDIT !
And while it's out of the scope of the question, compression is even more evil - and generally uses dictionary ;) - But compression are only effective from a certain amount of bytes....
A character is a graphical representation of a concept and may occupy an arbitrary number of bytes. For example, character "S" (capital letter 'S') occupies 1 byte whereas character 💋 (kissing lips) occupies 3 bytes.
I think your answer is wrong. byte is 1 character. a character in binary is a series of 8 on or offs or 0 or 1s. one of those is a bit and 8 bits make a byte so 1 byte is one character.so 2 bytes hold two characters.
It depends on the format of the string. 1 byte per character in ASCII and 2 bytes per character in Unicode. so 2 byte can hold only single Unicode character or 2 ASCII character.
The following code will explain my answer
MsgBox(System.Text.ASCIIEncoding.Unicode.GetByteCount("h")) '<--- displays 2
MsgBox(System.Text.ASCIIEncoding.ASCII.GetByteCount("h")) '<--- displays 1
This question already has answers here:
How to match multiple occurrences of a substring
(3 answers)
Closed 2 years ago.
Let's say I have an input field and want to parse all of the numbers from the submitted string. For example, it could be:
Hi I'm 12 years old.
How do I parse all of the numbers without having a common pattern to work with?
I tried:
x.match(/\d+/)
but it only grabs the 12 and won't go past the next space, which is problematic if the user inputs more numbers with spaces in-between them.
Add the g flag to return all matches in an array:
var matches = x.match(/\d+/g)
However, this may not catch numbers with seperators, like 1,000 or 0.123
You may want to update your regex to:
x.match(/[0-9 , \.]+/g)
var words = sentence.split(" ");
var numbers = words.filter(function(w) {
return w.match(/\d+/);
})
i have a word from A to Z. all word should in small latter (Capital not include) and 1 to 9 (included all special word who can be used in email address (just for a test)).
how i can generate unique 1 lacs text who never repeat itself. can anyone solve this puzzle.
i want a another thing that all words should not more then 10 char and not should minimum 6 char long
Put the characters in an array. Copy the array as the source of a new line. Randomly slice words from the array and put them in the line (use Math.random() * array.length | 0). Keep going for the required number of words.
You can also just use a string and charAt(index) if you only want single characters, but you have to keep cutting out the character that you select which is likely less efficient than using array.slice.
Whatever suits though, since performance is likely irrelevant.