简体   繁体   English

删除 Javascript 中的部分字符串

[英]Remove portion of string in Javascript

I have a String like this in Javascript:我在 Javascript 中有这样一个字符串:

string = '@usernameWhats on your mind?'

I need to get rid of 'Whats on your mind?'我需要摆脱'Whats on your mind?' from the string.从字符串。

How would I do that?我该怎么做?

var new_string = string.replace('Whats on your mind?', '');

At first, you can split the string:首先,您可以split字符串:

string.split("@username"); // ["", "What on your mind?"]

Then pop removes the last element.然后pop删除最后一个元素。

const newString = string.split("@username").pop();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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