Vue.js string indexOf


String indexOf Method– We can use native JavaScript indexOf to get the position of first occurrence of specified value. Here in this tutorial, we are going to explain how you can use this method to find the position of string/character in vuejs.


String indexOf Method In Vue.JS Example

You can get the position of a specified value in a string simply as below-

Example:

<!DOCTYPE html>
<html>
<head>
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.13/vue.js"></script>
</head>
<body>
<div id="app">
	{{result}}
</div>
<script>
 new Vue({
el: '#app',
  
  data: { 
	  str:"Hello World, welcome to tutorialsplane.com",
	  result: ''
  },
  
   methods:{
    myFunction: function () {
      var v = this;
	  var r = v.str.indexOf("welcome");
	  v.result = "Found at = "+r;
    }
	},
  mounted () {
  this.myFunction();
}

});
</script>        
</body>
</html>

Try it »

  • indexOf– This method returns the position of first occurrence of specified value.
  • It return -1 if the value is not found in string.

If you run the above example it will produce the output something like this-

Vue.js string indexOf JavaScript Example

Advertisements

Add Comment

📖 Read More