CodesJava

Easy learning with example program codes

Symbol.split JavaScript

The JavaScript Symbol.split property splits a part of the string from the indices that match the specified regular expression.

Syntax:

Symbol.split(string) 

Parameters:
string: It represents the string.

Returns:
It returns a string that split from the specified expression.

Example 1:

<!DOCTYPE html>
<html>
<body>
<script>    
class obj {  
constructor(a) {  
this.a = a;  }  
[Symbol.split](string)   
{  var index = string.indexOf(this.a);  
   return this.a;   }  }  
document.write('HELLO WORLD!'.split(new obj('WORLD')));   
</script>
</body>
</html>

Output:

WORLD

Example 2:

<!DOCTYPE html>
<html>
<body>
<script>  
class SplitTest {
  constructor(value) {
    this.value = value;
  }
  [Symbol.split](string) {
    var index = string.indexOf(this.value);
    return this.value + string.substr(0, index) + "/"
      + string.substr(index + this.value.length);
  }
}
 
document.write('codesjava'.split(new SplitTest('java')));
 
</script>
</body>
</html>

Output:

javacodes/
Please follow and like us:
error








Copyright © 2019 CodesJava DMCA.com Protection Status