split string at character js code example

Example 1: javascript split string

// Split string into an array of strings
const path = "/usr/home/chucknorris"
let result = path.split("/")
console.log(result)
// result
// ["", "usr", "home", "chucknorris"]
let username = result[3]
console.log(username)
// username
// "chucknorris"

Example 2: javascript separate string by char

var Str = "Hello There!"
var SplitOn = " "
var Results = Str.split(SplitOn);
//Results[0] will be 'Hello' and Results[1] will be 'There!'

Example 3: split() javascript

let countWords = function(sentence){
    return sentence.split(' ').length;
}

console.log(countWords('Type any sentence here'));

//result will be '4'(for words in the sentence)

Example 4: how to saperate string to array

Scanner in=new Scanner(System.in);
		String input=in.nextLine();
		String[] word=input.split(" ");