Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove a defined part of a string?

I have this string: "NT-DOM-NV\MTA" How can I delete the first part: "NT-DOM-NV\" To have this as result: "MTA"

How is it possible with RegEx?

like image 592
Tassisto Avatar asked Mar 15 '11 13:03

Tassisto


People also ask

How do I remove a specific part of a string?

To remove a substring from a string, call the replace() method, passing it the substring and an empty string as parameters, e.g. str. replace("example", "") . The replace() method will return a new string, where the first occurrence of the supplied substring is removed.

How do you delete a particular part of a string in Java?

replace() Method to Remove Substring in Java The first and most commonly used method to remove/replace any substring is the replace() method of Java String class. The first parameter is the substring to be replaced, and the second parameter is the new substring to replace the first parameter.


2 Answers

you can use this codes:

str = str.Substring (10); // to remove the first 10 characters. str = str.Remove (0, 10); // to remove the first 10 characters str = str.Replace ("NT-DOM-NV\\", ""); // to replace the specific text with blank  //  to delete anything before \  int i = str.IndexOf('\\'); if (i >= 0) str = str.SubString(i+1); 
like image 66
Fun Mun Pieng Avatar answered Sep 18 '22 07:09

Fun Mun Pieng


string.TrimStart(what_to_cut); // Will remove the what_to_cut from the string as long as the string starts with it.   

"asdasdfghj".TrimStart("asd" ); will result in "fghj".
"qwertyuiop".TrimStart("qwerty"); will result in "uiop".


public static System.String CutStart(this System.String s, System.String what) {     if (s.StartsWith(what))         return s.Substring(what.Length);     else         return s; } 

"asdasdfghj".CutStart("asd" ); will now result in "asdfghj".
"qwertyuiop".CutStart("qwerty"); will still result in "uiop".

like image 27
Vercas Avatar answered Sep 20 '22 07:09

Vercas