I have this string:
02-37-30-30-30-32-42-34-30-38-45-39-35-03
I want to remove the delimiter "-" so that the final output will be:
0237303030324234303845393503
How can I do that?
Try
myString.Replace ("-", "");
Tried this?
stringName.Replace("-", "");
This would replace all the -s with "" and will remove them.
If you want to use Regex, (although I'm not sure why that's beneficial) you can do it like this:
string result = Regex.Replace(
"02-37-30-30-30-32-42-34-30-38-45-39-35-03", "-", "");
Related
I need to remove \ character from imagepth string variable:
imagepth = "# Eval(\"Name\",\"Gallary/\"" + imgwords[4] + "\"/Images/{0}\")";
You can use Replace method to replace "\\" with a empty string
str = str.Replace("\\", "");
Regex Unescape will remove all escaping characters from a string, here is a link to this information:
http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.unescape.aspx
Here is a working syntax for me:
str = str.Replace("\"", string.Empty);
I have a text string, like this [A203][Tom D.][Local.VV-12], now i'm only intrested in the last text, [Local.VV-12], i'm able to delete everything using
string output = Regex.Replace(message, #" ?\[.*?\]", string.Empty);
but that deletes my last one too, how would i go about this?
Change your regex pattern:
string output = Regex.Replace(message, #" ?\[.*?\](?i:\[.*?\])", string.Empty);
Returns [Local.VV-12]
Try this using Substring function
string output=myString.Substring(myString.LastIndexOf("["));
How about this?
string output = Regex.Replace(message, #".*\[", "[");
I have read an entire file into a string object :-
string result = File.ReadAllText(#"C:\TestLog.log");
I want to remove all line breaks, "\n", but i also need to preserve any instances in the string object where "\r\n" exists.
How can i do this?
It looks like you want a regular expression replace.
string result = File.ReadAllText(#"C:\TestLog.log");
string newresult = Regex.Replace(result, #"[^\\r]\\n", "");
So the pattern looks for any \n that is not preceded by \r.
result = result.Replace("\n","").Replace("\r","\r\n")
Without using regex though. Not sure if you intend to use that or not.
I have the following strings:
This is my testasdasd [Test(XYZ="P")] abc sdfsdf
This is my testasdasd [Test(ABC="P")] sdfsdf
This is my testdfsdfsdf [Test(DEF="P")] sdfsdfs
This is my testsdfsdfsdf [Test(GHI="P")] asdfasdasd
I want the add ", Hello" text after the ")" inn the above strings. My output should look like this:
This is my testasdasd [Test(XYZ="P"), Hello] abc sdfsdf
This is my testasdasd [Test(ABC="P"), Hello] sdfsdf
This is my testdfsdfsdf [Test(DEF="P"), Hello] sdfsdfs
This is my testsdfsdfsdf [Test(GHI="P"), Hello] asdfasdasd
Can you help me making regex for that?
EDIT: I can't do the above just by doing find and replace "]" I have other brackets too in my strings. I need to find [Test(..)] and the output should be [Test(...) , Hello]
You can try with the lookup expression:
#"(\[\s*Test\s*\(.*?\)\s*)\]"
and this replace expression:
"$1,Hello]"
replace regex (?<=\[Test\(\w+?="P"\))\] with string , Hello]
Try this if you do not want to use a regex:
String newString = yourString.replace( "]", ", Hello]" );
This will only work correctly, if there is only one "]" in your string..
If the content is going to be static. Trying string.replace will do. My view :)
function insert(string, word) {
return string.replace(/\[Test\(.+\)\]/g, function(a, b) {
return a.replace("]", ", " + word + "]");
});
}
insert('This is my testasdasd [Test(XYZ="P")] abc sdfsdf', "Hello");
Try this regex:
(\[Test\([^\)]*\))\]
replace with
$1, Hello]
your code can be like this:
var result = Regex.Replace(inputString, #"(\[Test\([^\)]*\))\]", "$1, Hello]");
or this simle replace:
var result = inputString.replace( "]", ", Hello]" );
how can I format below string
Address1=+1234+block+of+XYZ+Street+Address2=+Santa+Fe+Springs+State=+California
to string
Address1=+1234+block+of+XYZ+Street+&Address2=+Santa+Fe+Springs+&State=+California
The below regex doesnt work properly.could someone fix this?
string inputString = "Address1=+1234+block+of+XYZ+Street+Address2=+Santa+Fe+Springs+State=+California";
string outString = Regex.Replace(inputString,#"([\s])([a-zA-Z0-9]*)(=)","&$1");
I think you want this
Regex.Replace(inputString,#"\+([a-zA-Z0-9]+)=","+&$1=")
Or this if you want to allow any character other than + & = in keywords.
Regex.Replace(inputString,#"\+([^+&=]+)=","+&$1=")
If all you want to do is prefix "Address2" and "State" by an ampersand:
Regex.Replace(inputString, "(?=Address2=|State=)", "&");