How to Replace a Substring of a String Before a Specific Character

How to remove all characters before a specific character in Java?

You can use .substring():

String s = "the text=text";
String s1 = s.substring(s.indexOf("=") + 1);
s1.trim();

then s1 contains everything after = in the original string.

s1.trim()

.trim() removes spaces before the first character (which isn't a whitespace, such as letters, numbers etc.) of a string (leading spaces) and also removes spaces after the last character (trailing spaces).

How to remove all characters before a specific character in Python?

Use re.sub. Just match all the chars upto I then replace the matched chars with I.

re.sub(r'^.*?I', 'I', stri)

How do I replace a substring of a string before a specific character?

You don't even need to use substring or replace, you can use this:

SELECT 'test' + RIGHT(email, charindex('@', REVERSE(email)))
FROM YourTable

You can test it out with this:

DECLARE @email nvarchar(50)
SET @email = 'carmine32@hotmail.com'
PRINT 'test' + RIGHT(@email, charindex('@', REVERSE(@email)))

Removing elements of string before a specific repeated character in it in javascript

It seems like you want to do some URL parsing here. JS brings the handful URL utility which can help you with this, and other similar tasks.

const myString = 'http://localhost:5000/contact-support';

const pathname = new URL(myString).pathname;

console.log(pathname); // outputs: /contact-support

// then you can also remove the first "/" character with `substring`
const whatIActuallyNeed = pathname.substring(1, pathname.length);

console.log(whatIActuallyNeed); // outputs: contact-support

How to replace a substring before and after a specific character in SQL Server?

Take the Substring before '[' add your replacement and the Substring right of ']'

declare @TSQL varchar(100)
declare @R varchar(100)
SET @TSQL = 'this is test string [this text may vary] other text'
SET @R = 'MySubstitute'
Select Left(@TSQL,Charindex('[',@TSQL)-1) + @R + RIGHT(@TSQL,Charindex(']',REVERSE(@TSQL))-1)

Replace after a string - Before a character

One option is to use Regex.Replace instead:

str = Regex.Replace(str, @"(?<=First Option:)[^.]*", "New text");

(?<=First Option:)[^.]* matches a sequence of zero or more characters other than dot '.', preceded by First Option: via a positive look-behind.

How to remove all characters from a string before a specific character

string A = "Hello_World";
string str = A.Substring(A.IndexOf('_') + 1);

javascript remove string before a specific character

The following loops through all h2 elements and removes the beginning via a regex replace:

window.onload = function() {
var h2s = document.getElementsByTagName("h2"),
i;

for (i=0; i < h2s.length; i++)
h2s[i].innerHTML = h2s[i].innerHTML.replace(/^[^-]+ - /,"");​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
};

Demo: http://jsfiddle.net/nGfYd/5/

The onload handler ensures that the code runs after the page has finished loading, because JavaScript is executed as it is encountered while the page is being parsed, but it can only manipulate elements that have been already been parsed. You don't need the onload handler if you include the code in a script block after the elements in question, in fact my preference is to put the <script> block at the end of the body just before the closing </body> tag and not use an onload handler.

EDIT: From comments under other answers it seems you are happy to use jQuery, in which case you could do this:

$(document).ready(function(){
$("h2").text(function(i, oldVal) {
return oldVal.replace(/^[^-]+ - /,"");
});
});

Demo: http://jsfiddle.net/nGfYd/7/

If you pass a function to .text() jQuery will call that function for each element, passing it the old (current) value of the element, so you don't need an .each() loop too.

(Again the document ready handler is not needed if you put the code in a script block at the end of the body.)

How to remove part of a string before a : in javascript?

There is no need for jQuery here, regular JavaScript will do:

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

Or, the .split() and .pop() version:

var str = "Abc: Lorem ipsum sit amet";
str = str.split(":").pop();

Or, the regex version (several variants of this):

var str = "Abc: Lorem ipsum sit amet";
str = /:(.+)/.exec(str)[1];


Related Topics



Leave a reply



Submit