If you look at the jsfiddle from question,
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.
To get the substring after a specific character, call the substring() method, passing it the index after the character's index as a parameter. The substring method will return the part of the string after the specified character.
Python Substring Before Character You can extract a substring from a string before a specific character using the rpartition() method. What is this? rpartition() method partitions the given string based on the last occurrence of the delimiter and it generates tuples that contain three elements where.
To split a string with specific character as delimiter in Java, call split() method on the string object, and pass the specific character as argument to the split() method. The method returns a String Array with the splits as elements in the array.
You can use the IndexOf method and the Substring method like so: string output = input. Substring(input. IndexOf('.
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(":"));
Where 'str' represents the variable with your string inside.
Click here for JSFiddle Example
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; }
Another method could be to split the string by ":" and then pop off the end. var newString = string.split(":").pop();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With