javascript 如何在某个字符之前返回部分字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16470113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 04:48:44  来源:igfitidea点击:

How to return part of string before a certain character?

javascriptsubstring

提问by Smudger

If you look at the jsfiddlefrom question,

如果你看的jsfiddle问题

var str = "Abc: Lorem ipsum sit amet";
str = str.substring(str.indexOf(":") + 1);

This returns all characters after the :, how can I adjust this to return all the characters before the :

这将返回 之后的所有字符:,我如何调整它以返回之前的所有字符:

something like var str_sub = str.substr(str.lastIndexOf(":")+1);but this does not work.

类似的东西, var str_sub = str.substr(str.lastIndexOf(":")+1);但这不起作用。

回答by Hexodus

You fiddle already does the job ... maybe you try to get the string beforethe double colon? (you really should edit your question) Then the code would go like this:

您已经完成了这项工作……也许您尝试在双冒号之前获取字符串?(你真的应该编辑你的问题)然后代码会是这样的:

str.substring(0, str.indexOf(":"));

str.substring(0, str.indexOf(":"));

Where 'str' represents the variable with your string inside.

其中 'str' 表示包含字符串的变量。

Click here for JSFiddle Example

单击此处查看 JSFiddle 示例

Javascript

Javascript

var input_string = document.getElementById('my-input').innerText;
var output_element = document.getElementById('my-output');

var left_text = input_string.substring(0, input_string.indexOf(":"));

output_element.innerText = left_text;

Html

html

<p>
  <h5>Input:</h5>
  <strong id="my-input">Left Text:Right Text</strong>
  <h5>Output:</h5>
  <strong id="my-output">XXX</strong>
</p>

CSS

CSS

body { font-family: Calibri, sans-serif; color:#555; }
h5 { margin-bottom: 0.8em; }
strong {
  width:90%;
  padding: 0.5em 1em;
  background-color: cyan;
}
#my-output { background-color: gold; }

回答by A. James McHale

Another method could be to split the string by ":" and then pop off the end. var newString = string.split(":").pop();

另一种方法可能是用“:”分割字符串,然后从末尾弹出。 var newString = string.split(":").pop();

回答by swapnil_nerd

And note that first argument of subString is 0 based while second is one based.

并注意 subString 的第一个参数是基于 0 的,而第二个是基于 1 的。

Example:

例子:

String str= "0123456";
String sbstr= str.substring(0,5);

Output will be sbstr= 01234and not sbstr = 012345

输出将是sbstr= 01234和不是sbstr = 012345

回答by best wishes

In General a function to return string after substring is

一般来说,在子字符串之后返回字符串的函数是

function getStringAfterSubstring(parentString, substring) {
    return parentString.substring(parentString.indexOf(substring) + substring.length)
}

function getStringBeforeSubstring(parentString, substring) {
    return parentString.substring(0, parentString.indexOf(substring))
}
console.log(getStringAfterSubstring('abcxyz123uvw', '123'))
console.log(getStringBeforeSubstring('abcxyz123uvw', '123'))