简体   繁体   中英

How can I delete the first word from a line?

Mon 25-Jul-2011

I want to delete the first word "Mon" with javascript jQuery. How can i do this?

If you don't want to split the string (faster, less memory consumed), you can use indexOf() with substr() :

var original = "Mon 25-Jul-2011";
var result = original.substr(original.indexOf(" ") + 1);
var string = "Mon 25-Jul-2011";
var parts = string.split(' ');
parts.shift(); // parts is modified to remove first word
var result;
if (parts instanceof Array) {
  result = parts.join(' ');
}
else {
  result = parts;
}
// result now contains all but the first word of the string.

I wanted to remove first word from each items in Array of strings. I did that using split , slice , join .

var str = "Mon 25-Jul-2011"
var newStr = str.split(' ').slice(1).join(' ')
console.log(str)

Run this code in console you will get the expected string.

You can manipulate any dom, using their reference id, class or tag. Example

<div id="date">Mon 25-Jul-2011</div>
<script>
$(document).ready(function() {
   var strDate = $('#date').html();
   // Using regex, this will remove any day which may present in your date DOM
   strDate.replace(/(mon|tue|wed|thu|fri|sat)/i, '');
   // This to trim any space present
   strDate.replace(/^\s+|\s+$/g,'');
   $('#date').html(strDate);
});
</script>
var str = "Mon 25-Jul-2011";
var firstSpace=str.indexOf(" ");
var newStr= str.slice(firstSpace);
//result:"25-Jul-2011"

easiest of all.

str.split(' ').slice(1).join(' ')

Another solution:

var line = "Mon 25-Jul-2011"; 
var edited = line.substring( line.indexOf(" ") + 1, line.length );

This should output "25-Jul-2011":

var string = "Mon 25-Jul-2011"; 
string = string.split(' ').pop();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM