且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-26 11:29:51

你已经完成了这项工作...也许你试着在双冒号之前得到字符串? (你真的应该编辑你的问题)然后代码将是这样的:

You fiddle already does the job ... maybe you try to get the string before the 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(":"));

其中'str'表示内置字符串的变量。

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

点击此处获取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

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

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; }