rtrim php equivalent in C# - c#

I would like to know how to accomplish the
PHP
query = rtrim($query, '& ');
in C# ASP .NET MVC
I have tried Strings.RTrim("string") but it does not work.
Have also tried query = query.Trim(query, "&"); but it gives me error.
Also msdn provides code for rtrim in visual basic, which does not help.
From looking at the code it seems like its' trimming the whitespace if found after & character.
Anyone know how to replicate the php command?
Cheers

The .Trim() method removes whitespaces from both the start and end of the string, so
" Hello World ".Trim() -> "Hello World"
For performing the RTrim() you can use the TrimEnd() method with the String.
" Hello World ".TrimEnd() -> " Hello World"

C# Has pretty neat String trimming methods.
I'm listing different options below. Although, the one you are looking for is
StringVariable.TrimEnd();
"This string ends here. ".TrimEnd(); //This also works.
String-trimming methods
string message = " Hello! ";
message.TrimEnd(); // Trims from the right side.
message.TrimStart(); // Trims from the left side.
message.Trim(); // Trims on both sides
Summary
message.TrimEnd() will trim from the right side of the string variable.
message.TrimStart() will trim from the left side of the string variable.
message.Trim() will trim from both sides of the string variable.
Also msdn provides code for rtrim in visual basic, which does not help.
I found a few that might help you, and they have them in multiple .NET languages, including C#.
Microsoft - String.Trim Method
Microsoft - String.TrimEnd Method
Microsoft - String.TrimStart Method
Hope this helps you :)

try this ==>
string.Remove(string.LastIndexOf("&"));
With the help of remove last index you can remove the last element

Related

NSDK Skip instruction?

Sorry in advance for the inappropriate tag (this is more a NSDK issue than a C# issue, but NSDK tag wasn't existing, and I couldn't create it so I had to choose one...)
I'm currently writting a webservice using C#.
My goal is to reproduce what a NSDK code is doing.
Everything is going ok so far but one thing.
I have absolutely no clue about what the skip instruction is doing.
Here is an exemple of an instruction
if skip SomeString <> ''
I know this is testing if someString is empty or not, but the "skip" makes me wonder what it does.
The main goal of my webservice is to create a file, and to send it to a printer after a writing phase and I need to be vary careful with spaces or backlines and stuff so if someone could explain this to me, I'd gladly appreciate!
Best Regards.
Got my Answer.
skip instruction is removing spaces at the beginning and the end of the string.
For instance :
VALUE$ = "  key "
There is 2 spaces before and 1 space after the value.
length(VALUE$) gives 6 (the 3 spaces + the three letters)
length(skip VALUE$) gives 3 (only the three letters)
So skip VALUE$ equals "key", and not "  key "

How to produce a soft return using C#.net

I know this is kind of easy question but i cant seem to find it anywhere. Is there someone out there who knows how to create a soft return inside a set of text using C#.net?
I need to print soft return to a text file/xml file. this text file will be generated using c#.net. you could verify if the answer is correct if you use NOTEPAD++ then enable the option to “View>Show Symbol > Show End of Line” then you will see a symbol like this:
Thanks in advance :)
Not sure what you mean by a soft return. A quick Google search says it's a non-stored line break typically due to word wrapping in which case you wouldn't actually put this in a string, it would only be relevant when the string was rendered for display.
To put a carriage return and/or line feed in the string you would use:
string s = "line one\r\nline two";
And for further reference, here are the other escape codes that you can use.
Link (MSDN Blogs)
In response to your edit
The LF that you see can be represented with \n in a string. Obviously you have a specific line ending sequence that you need to represent. If you were to use Environment.NewLine that is going to give you different results on different platforms.
var message = $"Tom{Convert.ToChar(10)}Harry";
Results in:
Tom
Harry
With just a line feed between.
Lke already mentioned you can use Enviroment.NewLine but I am not sure if that i what you want or if you are actually trying to append a ASCII 141 to your string as mentioned in the comments.
You can add ASCII chr sequences to your string like this.
var myString = new StringBuilder("Foo");
myString.Append((char)141);

String.Replace not working as expected

I have the following problem with String.Replace:
string str = "0˄0";
str = str.Replace("˄", "&&");
when I print out str, it will be "0&0" instead of "0&&0". To get "0&&0", I have to write
str = str.Replace("˄", "&&&&");
Why is that?
& is a special character in WinForms, used to indicate keyboard shortcuts. Using && is how WinForms escapes the & symbol.
So, to display it in WinForms you are necessarily going to have to place two & characters in your string as you have here:
str = str.Replace("˄", "&&&&");
This is strictly a WinForms "thing" and has nothing to do with a C# or .NET string escaping specifically. It has been this way at least as far back as Visual Basic 4 - probably before then.
You may want to check out this post:
http://moiashvin-tech.blogspot.com/2008/05/escape-ampersand-character-in-c.html
Labels are handled differently in WinForms. You should be able to do as the post suggests and set the UseMnemonic property to false, see if that works.
The answer is in the first comment - '&' is a special character for WinForms (and the underlying Win32 API) which is/was used to indicate a shortcut character for menu/dialog items.
'&' in strings means nothing special to C#, but if you want to put it on a form/dialog label, then you need to escape the '&' by adding another one in front.
Personally, I would get the string how I really wanted it in my 'business logic' first, then escape the '&' characters as part of displaying the string on the form.

Caveats Encoding a C# string to a Javascript string

I'm trying to write a custom Javascript MVC3 Helper class foe my project, and one of the methods is supposed to escape C# strings to Javascript strings.
I know C# strings are UTF-16 encoded, and Javascript strings also seem to be UTF-16. No problem here.
I know some characters like backslash, single quotes or double quotes must be backslash-escaped on Javascript so:
\ becomes \\
' becomes \'
" becomes \"
Is there any other caveat I must be aware of before writing my conversion method ?
EDIT:
Great answers so far, I'm adding some references from the answers in the question to help others in the future.
Alex K. suggested using System.Web.HttpUtility.JavaScriptStringEncode, which I marked as the right answer for me, because I'm using .Net 4. But this function is not available to previous .Net versions, so I'm adding some other resources here:
CR becomes \r // Javascript string cannot be broke into more than 1 line
LF becomes \n // Javascript string cannot be broke into more than 1 line
TAB becomes \t
Control characters must be Hex-Escaped
JP Richardson gave an interesting link informing that Javascript uses UCS-2, which is a subset of UTF-16, but how to encode this correctly is an entirely new question.
LukeH on the comments below reminded the CR, LF and TAB chars, and that reminded me of the control chars (BEEP, NULL, ACK, etc...).
(.net 4) You can;
System.Web.HttpUtility.JavaScriptStringEncode(#"aa\bb ""cc"" dd\tee", true);
==
"aa\\bb \"cc\" dd\\tee"
It's my understanding that you do have to be careful, as JavaScript is not UTF-16, rather, it's UCS-2 which I believe is a subset of UTF-16. What this means for you, is that any character that is represented than a higher code point of 2 bytes (0xFFFF) could give you problems in JavaScript.
In summary, under the covers, the engine may use UTF-16, but it only exposes UCS-2 like methods.
Great article on the issue:
http://mathiasbynens.be/notes/javascript-encoding
Just use Microsoft.JScript.GlobalObject.escape
Found it here: http://forums.asp.net/p/1308104/4468088.aspx/1?Re+C+equivalent+of+JavaScript+escape+
Instead of using JavaScriptStringEncode() method, you can encode server side using:
HttpUtility.UrlEncode()
When you need to read the encoded string client side, you have to call unescape() javascript function before using the string.

How can I deal with ampersands in a mail client's mailto links?

I have an ASP.NET/C# application, part of which converts WWW links to mailto links in an HTML email.
For example, if I have a link such as:
www.site.com
It gets rewritten as:
mailto:my#address.com?Subject=www.site.com
This works extremely well, until I run into URLs with ampersands, which then causes the subject to be truncated.
For example the link:
www.site.com?val1=a&val2=b
Shows up as:
mailto:my#address.com?Subject=www.site.com?val1=a&val2=b
Which is exactly what I want, but then when clicked, it creates a message with:
subject=www.site.com?val1=a
Which has dropped the &val2, which makes sense as & is the delimiter in a mailto command.
So, I have tried various other was to work around this with no success.
I have tried implicitly quoting the subject='' part and that did nothing.
I (in C#) replace '&' with & which Live Mail and Thunderbird just turn back into:
www.site.com?val1=a&val2=b
I replaced '&' with '%26' which resulted in:
mailto:my#address.com?Subject=www.site.com?val1=a%26amp;val2=b
In the mail with the subject:
www.site.com?val1=a&val2=b
EDIT:
In response to how URL is being built, this is much trimmed down but is the gist of it. In place of the att.Value.Replace I have tried System.Web.HtmlUtility.URLEncode calls which also results in a failure
HtmlAgilityPack.HtmlNodeCollection nodes =doc.DocumentNode.SelectNodes("//a[#href]");
foreach (HtmlAgilityPack.HtmlNode link in nodes)
{
HtmlAgilityPack.HtmlAttribute att = link.Attributes["href"];
att.Value = att.Value.Replace("&", "%26");
}
Try mailto:my#address.com?Subject=www.site.com?val1=a%26val2=b
& is an HTML escape code, whereas %26 is a URL escape code. Since it's a URL, that's all you need.
EDIT: I figured that's how you were building your URL. Don't build URLs that way! You need to get the %26 in there before you let anything else parse or escape it. If you really must do it this way (which you really should try to avoid), then you should search for "&" instead of just "&" because the string has already been HTML escaped at this point.
So, ideally, you build your URL properly before it's HTML escaped. If you can't do it properly, at least search for the right string instead of the wrong one. "&" is the wrong one.
You cant put any character as subject. You could try using System.Web.HttpUtility.URLEncode function on the subject´s value...
Using the URL escape code %26 is the right way.
Sadly this is still not working on the Android OS because of bug 8023
What I ended up doing for my case was eliminating the &.
www.site.com/mytest.php?val1=a=b=c. Where the 2nd and 3rd = would be equivalent to www.site.com?val1=a&val2=b&val3=c
In mytest.php I explode on ? and then explode again on =.
A total hack I know but it does work for me.

Categories

Resources