/* WordSplit.js - Enhanced string splitting function similar to JavaScript's .split() 2020 - 2022 by mercury0x0d, Creative Commons Attribution 4.0 International License Revision history: 2022-08-12 - Updated years in license string, updated description 2022-05-25 - Corrected typo in the description of WordSplit() 2022-04-02 - Corrected typo in the description of WordGet(), corrected description of WordSplit() 2020-05-10 - Added null and undefined checking to all functions 2020-04-26 - Added licensing 2020-03-11 - Initial release */ function WordCount(theString, separatorList) { // Returns the number of words i the string specified according to the list of separator characters specified. var currentChar = "", lastType = 0, mainStrLen = 0, position = 0, wordCount = 0; const nonSeparator = 1, isSeparator = 2; // exit if the string is null or undefined if (theString == '' || typeof theString == 'undefined') return; // get the length of the input string mainStrLen = theString.length; // exit if the string was empty if (!mainStrLen) return 0; do { currentChar = theString.charAt(position++); if (separatorList.includes(currentChar)) { // this character is a separator lastType = isSeparator; } else { // this character is not a separator // if the last character was not a non-separator, increment the word count if (lastType != nonSeparator) wordCount++; lastType = nonSeparator; } } while (position < mainStrLen) return wordCount; } function WordGet(theString, separatorList, wordNum) { // Returns the number of words in the string specified according to the list of separator characters specified. var currentChar = "", wordReturned = "", lastType = 0, mainStrLen = 0, position = 0, wordCount = 0; const nonSeparator = 1, isSeparator = 2; // exit if the string is null or undefined if (theString == '' || typeof theString == 'undefined') return; // get the length of the input string mainStrLen = theString.length; // exit if the string was empty or the requested word is zero if (!mainStrLen | wordNum == 0) return ""; do { currentChar = theString.charAt(position++); if (separatorList.includes(currentChar)) { // this character is a separator // see if we have the word requested and exit if so if (wordNum == wordCount) return wordReturned; // if we get here, wordReturned needs cleared wordReturned = ""; // note that this char was a separator lastType = isSeparator; } else { // this character is not a separator // if the last character was not a non-separator, increment the word count if (lastType != nonSeparator) {wordCount++;} // note that this char was not a separator lastType = nonSeparator; // add this character to wordReturned wordReturned = wordReturned + currentChar; } } while (position < mainStrLen) // check to see if the word requested was greater than whatever word we're on now and zero out the string if so // this prevents returning weird results for requested word numbers which are greater than the number of words in the string if (wordNum > wordCount) wordReturned = ""; return wordReturned; } function WordSplit(theString, separatorList, wordArray) { // Splits the string given by the seperators in the string separatorList and returns the result in wordArray var currentChar = "", wordReturned = "", lastType = 0, mainStrLen = 0, position = 0; const nonSeparator = 1, isSeparator = 2; // exit if the string is null or undefined if (theString == '' || typeof theString == 'undefined') return; // zero the output array wordArray.length = 0; // get the length of the input string mainStrLen = theString.length; // if the string is empty, we don't need to do a thing! if (mainStrLen); { do { currentChar = theString.charAt(position++); if (separatorList.includes(currentChar)) { // this character is a separator // if the word we have is not null, add it to the list and clear wordReturned if (wordReturned != '') wordArray[wordArray.length] = wordReturned; wordReturned = ""; // note that this char was a separator lastType = isSeparator; } else { // this character is not a separator // note that this char was not a separator lastType = nonSeparator; // add this character to wordReturned wordReturned = wordReturned + currentChar; } } while (position <= mainStrLen) } }