Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   
   HTML   
   CSS   
   JAVA SCRIPT   
   JQUERY   




split() - FUNCTION


split() Function


The split() Function is used to split a String into an Array.


Example :



<html>
<body>  
<script language = "javascript" type = "text/javascript">
		
	var x = "Hello Beautiful World"
	var y = x.split(' ')
	document.write(y)
    
</script>      
</body>
</html>


Output :



  Hello: Beautiful: World:

So, in the above code, we have a String Hello Beautiful World initialised to a variable x.


var x = "Hello Beautiful World"
java_Collections


Then we have used the split() function on the variable x.


var y = x.split(' ')

So, the split() function converts the String, Hello Beautiful World to a Array, separated by a space (i.e. ' ').


And initialises the Array to the variable y.

java_Collections

So, if you see the above String, Hello Beautiful World. Hello, Beautiful and World are separated by a space (i.e. ' ').


So, a Array is formed with Hello, Beautiful and World.


But what if the Strings are separated by @ or any other symbol.


Say, the below String is separated by @.


"Hello@Beautiful@World"

Now, if you want to form a Array with Hello, Beautiful and World. You can use split() function providing the separator @ as argument.


Example :



<html>
<body>  
<script language = "javascript" type = "text/javascript">
		   
	var x = "Hello@Beautiful@World"
	var y = x.split("@")
    
    for (i of y) {
		document.write(i, ": ")
    }    
    
</script>      
</body>
</html>


Output :



  Hello: Beautiful: World:

So, in the above code, we have a String Hello@Beautiful@World initialised to a variable x.


var x = "Hello@Beautiful@World"
java_Collections


Then we have used the split() function with the separator @ as argument on the variable x.


var y = x.split("@")

So, the split() function converts the String, Hello@Beautiful@World to a Array, separated by the separator (i.e. @).


And initialises the Array to the variable y.

java_Collections

In the above example, we have three items in the Array. But what if, you want only two items in the Array.


i.e. For the same String, Hello@Beautiful@World, we just want two items in the Array, Hello and Beautiful

java_Collections

We can achieve the above using a second parameter that specifies the number of Array elements.


split() Function using separator and number of Array elements as arguments


Example :



<html>
<body>  
<script language = "javascript" type = "text/javascript">
		   
	var x = "Hello@Beautiful@World"
	var y = x.split("@", 2)
    
    for (i of y) {
		document.write(i, ": ")
    }    
    
</script>      
</body>
</html>


Output :



  Hello: Beautiful:

So, in the above code, we have a String Hello@Beautiful@World initialised to a variable x.

java_Collections

Then we have used the split() function with the separator @ and 2 as arguments on the variable x.


var y = x.split("@", 2)

So, the split() function converts the String, Hello@Beautiful@World to a Array, separated by the separator (i.e. @).


And initialises the Array to the variable y.

java_Collections

And as we can see only Hello and Beautiful is taken omitting World.