JavaScript String split() function
The split() method is used to split a string into an array of substrings, and returns the new array.
var str = "ramesh suresh mahesh roshan";
var res = str.split(" ");
The result of res will be an array with the values:
ramesh,suresh,mahesh,roshan
Definition: The split() method is used to split a string into an array of substrings, and returns the new array.
Tip: If an empty string ("") is used as the separator, the string is split between each character.
Note: The split() method does not change the original string.
Syntax:string.split(separator,limit)
Parameter Values :
- separator - Optional. Specifies the character, or the regular expression, to use for splitting the string. If omitted, the entire string will be returned (an array with only one item).
- limit- Optional. An integer that specifies the number of splits, items after the split limit will not be included in the array.
Answer to your question:
As many folks already given so many correct answer still I wanted to give some light from my end.
JavaScript Based Answer
function fnStringSplitter(fullName,separator){
var mName = ""; //Middle Name
var splittedArr = fullName.split(separator);
for (var i = 1; i <= splittedArr.length - 2; i++) {
mName = mName + " " + splittedArr[i];
}
alert("First Name : " + splittedArr[0]);
alert("Middle Name : " + mName);
alert("Last Name : " + splittedArr[splittedArr.length - 1]);
}
Calling to function: fnStringSplitter("ramesh suresh mahesh roshan"," ");
Output: 3 alerts with first,middle and last name.