Remove specific string from given URL - c#

I have a URL example image1-resize.jpg, and I want to delete -resize and save image1.jpg in new variable.
How can I do that?
This is what I tried to do:
str1 += "<li><a href='#pic" + counter + "'><img src='admin/temp/hotelimg/" + temp_url.ToString() + "'/></a></li>";
string stt =temp_url.replace("-resize","");
str2 += "<div id='pic" + counter + "'><img src='admin/temp/hotelimg/" + stt.ToString() + "' width='550' height='370'/></div>";

This should do your job:
temp_url.replace("-resize","");
Note: You should always search before putting questions here, since sometimes its really easy and need just small research on it.

Related

QueryString is taking first substring and discarding rest post space

I have a query string which passes 6 parameters in C# as shown below
string url = "Report.aspx?Desc=" + Desc.SelectedValue + "&PON=" + PNumber.Text + "&InsNme=" + ins.ToUpper().ToString() + "&BackTy=" + cb.SelectedValue + "&StartDate=" + txtDate.Text + "&EndDate=" + txtTodate.Text + "&Name=" + nme;
string s = "window.open('" + url + "', 'popup_window', 'width=1500,height=800,left=200,top=150,resizable=yes');";
ClientScript.RegisterStartupScript(this.GetType(), "script", s, true);
Now, in the above string InsNme contains a value of John Patrice Joanne. Instead of complete value of InsNme Report.aspx contains just John. How to handle this?
The spaces in the name are breaking the URL.
If you want to do it yourself, replace spaces with %20. Otherwise a simple, but not anywhere near "good" technique is:
url = "Report.aspx?";
// for each name value pair ...
url += dataLabel + "=" + System.Web.HttpUtility.UrlEncode( dataChunk ) +"&";
The utility is preferred as it will take care of other, similar issues such as literal '&' in a name.
Check this answer for better solutions.
How to build a query string for a URL in C#?

How to give url properly to anchor tag in asp.net using jquery?

I have written jquery in .net application as
Function UploadComplete(sender, args) {
var filename = args.get_fileName();
var contentType = args.get_contentType();
var folder = "~/Uploads/";
var text = "Size of " + filename + " is " + args.get_length() + " bytes";
if (contentType.length > 0) {
text += "and content type is '" + contentType + "'.";
text += "<a href='" + folder + filename + "'" + filename + "</a>";
}
document.getElementById('lblStatus').innerText = text;
}
Now my issue is I am not able to give path accurately in the line
text += "<a href='" + folder + filename + "'" + filename + "</a>";
Please help me!!!
A few issues to sort out:
1) "~/"
"~/" as a relative path is for use server-side in .Net. Javascript (really your browser) does not know what to do with that URL.
As a little trick, you can inject an application root URL using this Razor code #(Url.Content("~/")) but that means your function needs to be in a razor page and not a separate JS file.
If your JS is "elsewhere", inject the path as a Javascript variable (e.g. window.rootUrl = "#(Url.Content("~/"))") using a small script section on the page.
2) Missing '>'
You are missing a closing > in your generated anchor.
3) innerHTML, not innerText
You need to set the innerHTML property, or you will get raw text. You will probably need to tweak the formatting of the output (line breaks or spans/paragraphs) to make it look pretty (e.g. you have no space before the anchor at the moment).
Put it all together and you get something like:
function UploadComplete(sender, args) {
var filename = args.get_fileName();
var contentType = args.get_contentType();
var folder = '#(Url.Content("~/"))Uploads/';
var text = 'Size of ' + filename + ' is ' + args.get_length() + ' bytes';
if (contentType.length > 0) {
text += 'and content type is "' + contentType + '". ';
text += '' + filename + '';
}
document.getElementById('lblStatus').innerHTML = text;
}
As I said in comment, I strongly recommend using the single quote as a string delimiter in jQuery/JavaScript so that any HTML strings have double-quotes on attributes.
4) jQuery?
You tagged the question with jQuery too, which would shorten the last line to:
$('#lblStatus').html(text);
Apologies for any typos, I typed all this off the top of my head and did not verify it.

In html / asp.net-mvc, what is the correct way to include an apostrophe inside an image tooltip

If i have an image tooltip that is being populated from a database table. I am generating this html below from my server side C# code
public string GetImage()
{
return "<img class='iconSpace' title ='" + dataIssue + "' src='/Content/Images/Icons" + size + "/information_red.png' />";
}
the issue is that if the variable dataIssue has an apostrophe in it, it only shows the characters in the string up to that point.
What is the best way to show the whole string in the tooltip given the code above?
' is not special symbol for HTML, and browser shows whole string without problems, but you can have problems with following symbols " < > & they should be escaped as:
"
<
>
&
if your browser treats HTML standard incorrectly and cut the rest of the string, you can try to escape single quote with ' - this will work for all browsers
so, according HTML standard attribute values should be surrounded by " symbol, not by ', so the problem here should be solved:
dataIssue = any_kind_of_html_escape_function_here(dataIssue);
return "<img class=\"iconSpace\" title=\"" + dataIssue + "\" src=\"/Content/Images/Icons" + size + "/information_red.png\" />";
For asp.net htmlencode function is defined here: http://msdn.microsoft.com/en-us/library/w3te6wfz.aspx
Would this work for you?
string img = "<img class=\"iconSpac\" title=\"" + dataIssue + "\" " + "scr=\"/Content/Images/Icons\"" + size + "/information_red.png\" />";
You should use HttpUtility.HtmlEncode("...") for it.
http://msdn.microsoft.com/en-us/library/73z22y6h.aspx

How can I elegantly implement multiple string replacements in the same file?

Currently I have some code to replace strings in a file that looks like this:
File.WriteAllText(filePath, Regex.Replace(File.ReadAllText(filePath),
"( " + column.Key + " )",
" " + column.Value + " "
));
File.WriteAllText(filePath, Regex.Replace(File.ReadAllText(filePath),
"(\\[\"" + column.Key + "\"\\])",
"[\"" + column.Value + "\"]"
));
However, each replacement opens and closes the file, and it seems that occasionally they run "too fast" and one replacement will not work because the file did not close yet in a previous string replacement. Is there any code I can reuse that solves this issue, perhaps using the FileStream class (so I can open and close once)? Or suggestions on a better way to do this? Just wondering if there is something simpler I can do than having to create byte arrays of the strings I want to replace and writing code to read, write, and seek through bytes manually. Thanks.
A better practice would be to read the contents of the file once, storing it into a local variable. Then performing any changes you need (in your case, two regular expressions), and then writing that output to the file. File IO is one of the most expensive operations a computer can perform, and in-memory computation is much cheaper. Hit the disk as little as possible, as long as you can avoid it.
Well, I'd use:
string text = File.ReadAllText(filePath);
text = Regex.Replace(...);
text = Regex.Replace(...);
...
File.WriteAllText(filePath, text);
I'm still surprised to hear that the original code didn't work though. It wasn't pleasant in terms of multiple writes and reads, but I'd have expected it to work.
string contents = File.ReadAllText(filePath);
contents = Regex.Replace(contents,
"( " + column.Key + " )",
" " + column.Value + " ");
contents = Regex.Replace(contents,
"(\\[\"" + column.Key + "\"\\])",
"[\"" + column.Value + "\"]");
File.WriteAllText(filePath, contents);
Sounds like you should be doing your all your string replacements on a string residing in memory, then write your final resulting string to disk.
var fileContents = File.ReadAllText(filePath);
fileContents = Regex.Replace(fileContents,
"( " + column.Key + " )",
" " + column.Value + " "
);
fileContents = Regex.Replace(fileContents ,
"(\\[\"" + column.Key + "\"\\])",
"[\"" + column.Value + "\"]"
);
File.WriteAllText(filePath, fileContents);
Well, the simlest method would be to ReadAllText, do your replacements and then WriteAllText.
var text = File.ReadAllText(filePath);
text = Regex.Replace(text,"( " + column.Key + " )"," " + column.Value + " ");
text = Regex.Replace(text,"(\\[\"" + column.Key + "\"\\])","[\"" + column.Value + "\"]");
File.WriteAllText(text,filePath);

String woes in C# - having a . next to a "

I have the following string:
string text = #"" + reportName + "<br/><br/>";
When this string gets written to my page, it looks like this:
DR - 3: Debt Payment History
Can someone help explain why the period id being escaped? I'm fumbling with how to escape this properly.
Thanks.
I'm not sure exactly what your output should look like but
String.Format and escaping are your friend.
string text = String.Format("<a href='http://raustdsx0700.real.local:7782/analytics/saw.dll?PortalPages&PortalPath={0}&Page={1}&P0=1&P1=eq&P2=Project.\"Project Name\"'' target='_blank'>{1}</a><br/><br/>",dashboardURL,reportName);
If you are intent on using quotes in your URL, then use single-quotes for your tag attributes.
string text = "<a href='http://raustdsx0700.real.local:7782/analytics/saw.dll?PortalPages&PortalPath=" + dashboardURL + "&Page=" + reportName + "&P0=1&P1=eq&P2=Project.\"Project Name\"' target='_blank'>" + reportName + "</a><br/><br/>";
Or better, url-escape the quote (%22):
string text = "<a href='http://raustdsx0700.real.local:7782/analytics/saw.dll?PortalPages&PortalPath=" + dashboardURL + "&Page=" + reportName + "&P0=1&P1=eq&P2=Project.%22Project Name%22' target='_blank'>" + reportName + "</a><br/><br/>";
You need to urlencode your string, check this out: http://msdn.microsoft.com/en-us/library/zttxte6w.aspx

Categories

Resources