Use Replace function to remove "{ characters from string - asp.net

I would like to remove "{ and replace it with {. The following is the line of code that I'm currently using.
var MyString = DataString.Replace(#""{", "");
The following is the error message I'm getting
Please advise
Thanks

You need to escape the quote that you want to replace using two quotes, so for your example:
var MyString = DataString.Replace(#"""{", "{");
Also see How to include quotes in a string for alternatives to use quotes in strings.

If you are expecting JSON data then what you really need is a JSON Parser for that. And if you just want to replace "{ to { then you simply need to escape and replace the string like below:
// Suppose the variable named str has a value of Hello"{ wrapped in double quotes
var strReplaced = str.Replace("\"{", "{");
Console.WriteLine($"strReplaced: {strReplaced}");
// This will result in strReplaced: Hello{

Related

asp.net querystring

I have the following page querystring:
register.aspx?id="jSmith"
I have the following code to retrieve the value of ID
string qString = string.IsNullOrEmpty(Request.QueryString["id"]) ? string.Empty : HttpUtility.UrlDecode(Request.QueryString["id"]);
When I view the value of qString I get something like
"\"jSmith\""
so when I do the following:
if (qString == "jSmith")
{
........
}
it does not not execute the if condition. What do I need to do s that it does not have the quotes.
The code is correct.
The problem is that you are passing to the page "jSmith" with the double quotes as part of the string.
Try invoke the page this way
register.aspx?id=jSmith
That is because the correct way to give the path in this case would be register.aspx?id=jSmith, without the quotes. If you need spaces, or other special characters, in your ID, these should be URL encoded (and will be decoded by your code), but not enclosed in quotes.
For example, if your id was the string john smith, the URL would become register.aspx?id=john+smith, since + is the URL encoding of a space.
You don't need to put quotes around values in querystring, by definition they're all strings...
Your querystring should look like :
register.aspx?id=jSmith
You do not need the quotation marks in your querystring.
It should read
register.aspx?id=jSmith
You should look for
if (qString == "\"jSmith\"")
the \ is escaping the extra "
or you could perform a replace to remove the extra "
use
Response.Redirect("Qstring.aspx?name= smith");
and on the page Qstring.aspx load event
string s=Request.QueryString["name"].ToString();
gives u "smith" in s variable

how to pass parameter inside Strigbuilder class object?

I am using
string strurl = "Reports/ReportFilter.aspx";
and bind a tag as
AnchorLeftMenuLinks.Append(" href='javascript:OpenDialogue(" + strurl + ");' ");
but it return error as "undefined object AuditReports" as runtime it become like
href="javascript:OpenDialogue(Reports/ReportFilter.aspx);"
but when i add single quotes manually in firebug like
href="javascript:OpenDialogue('Reports/ReportFilter.aspx');"
it works fine.
can anyone suggest me that how to add single quotes in code.Yhankx in advance.
Try this
AnchorLeftMenuLinks.Append(" href='javascript:OpenDialogue(\"" + strurl + "\");' ");
Try:
var javascript = string.Format("href='javascript:OpenDialouge('{0}');'", strurl);
AnchorLeftMenuLinks.Append(javascript);
or:
AnchorLeftMenuLinks.AppendFormat("href='javascript:OpenDialouge('{0}');'", strurl);
Reason behind it was Javascript String because In JavaScript, a string is started and stopped with either single or double quotes. This means that the string was being chopped to: javascript:OpenDialogue( and your function's syntax was being incorrect and thus it was not working.
Thus it was mandatory to place a backslash (\)before each double quote in strurl. This turns each double quote into a string literal.
There are some other special characters also which needed to be placed using \
\' - single quote
\" - Double Quote
\\ - BackSlash
\n - new Line
\t - tab

Html ahref tag in stringbuilder

I am using html
strBody.Append("<span style=\"font-family:Arial;font-size:10pt\"> Hi " + Name + ",<br/><br/> Welcome! <br/><br/>");
strBody.Append("<tr><td style=\"font-weight:bold\">");
strBody.Append("documents for reference are shared in the Account Induction Portal ");
strBody.Append("</td><td>");
strBody.Append("Visit W3Schools<br/><br/>");
strBody.Append("</td><td>");
strBody.Append("</td></tr>");
strBody.Append("<tbody/></table><br/>");
Here href got error i cant include that in string bulider append without error.Pls help on this
you have two sets of parenthesis you must small quote for the url!
strBody.Append("<a href='http://www.w3schools.com'>Visit W3Schools</a><br/><br/>");
or escape like
strBody.Append("Visit W3Schools<br/><br/>"
You don't escape the quotes (") in the following line:
// Replace this
strBody.Append("Visit W3Schools<br/><br/>");
// with either this:
strBody.Append("Visit W3Schools<br/><br/>");
// or use single quotes inside the string:
strBody.Append("<a href='http://www.w3schools.com'>Visit W3Schools</a><br/><br/>");

What is the regular expression for "No quotes in a string"?

I am trying to write a regular expression that doesn't allow single or double quotes in a string (could be single line or multiline string). Based on my last question, I wrote like this ^(?:(?!"|').)*$, but it is not working. Really appreciate if anybody could help me out here.
Just use a character class that excludes quotes:
^[^'"]*$
(Within the [] character class specifier, the ^ prefix inverts the specification, so [^'"] means any character that isn't a ' or ".)
Just use a regex that matches for quotes, and then negate the match result:
var regex = new Regex("\"|'");
bool noQuotes = !regex.IsMatch("My string without quotes");
Try this:
string myStr = "foo'baa";
bool HasQuotes = myStr.Contains("'") || myStr.Contains("\""); //faster solution , I think.
bool HasQuotes2 = Regex.IsMatch(myStr, "['\"]");
if (!HasQuotes)
{
//not has quotes..
}
This regular expression below, allows alphanumeric and all special characters except quotes(' and "")
#"^[a-zA-Z-0-9~+:;,/#&_#*%$!()\[\] ]*$"
You can use it like
[RegularExpression(#"^[a-zA-Z-0-9~+:;,/#&_#*%$!()**\[\]** ]*$", ErrorMessage = "Should not allow quotes")]
here use escape sequence() for []. Since its not showing in this post

How to delete part of a string in actionscript?

So my string is something like "BlaBlaBlaDDDaaa2aaa345" I want to get rid of its sub string which is "BlaBlaBlaDDD" so the result of operation will be a string "aaa2aaa345" How to perform such thing with actionscript?
I'd just use the String#replace method with a regular expression:
var string:String = "BlaBlaBlaDDD12345";
var newString:String = string.replace(/[a-zA-Z]+/, ""); // "12345"
That would remove all word characters. If you're looking for more complex regular expressions, I'd mess around with the online Rubular regular expression tester.
This would remove all non-digit characters:
var newString:String = string.replace(/[^\d]+/, ""); // "12345"
If you know the exact string you want to remove, then just do this:
var newString:String = string.replace("BlaBlaBlaDDD", "");
If you have a list (array) of substrings you want to remove, just loop through them and call the string.replace method for each.

Resources