What will be the equivalent code for Format(iCryptedByte, "000") (VB.NET) in C# ?
String.Format(format, iCryptedByte); // where format like {0:D2}
See MSDN 1, 2, 3
Another very useful site for C# string formatting: http://blog.stevex.net/string-formatting-in-csharp/
Instead of {0:D3} you can also use the zero placeholder, e.g. {0:000} will pad with zeros to minimum length of three.
Microsoft.VisualBasic.Strings.Format(iCryptedByte, "000");
You'll need to add a reference to the Microsoft.VisualBasic assembly.
Given this VB code:
Strings.Format(iCryptedByte, format)
Replace with this C# code:
var csformat = "{0:" + format + "}";
String.Format(csformat, iCryptedByte);
Try:
iCryptedByte.ToString("D3");
see String.Format
Related
in persian/arabic character, some character used optional on top or bottom of other character like ِ َ ّ ُ.
in my example if i use this character, indexOf not found my word. consider that persian/arabic is rtl language.
for example:
منّم => م + ن + ّ + م
C#:
"منّم".IndexOf("من");
return -1
javascript:
var index= ' منّم '.indexOf('من');
console.log(index);
what happened in C#. anyone can explain this?
By passing in StringComparison.Ordinal as an argument to the overloaded String.IndexOf(), you could have also done the following:
"منّم".IndexOf("من", StringComparison.Ordinal); // returns 0
Specifying CompareOptions.Ordinal as an option should work, together with the IndexOf method of CompareInfo.
CompareInfo info = CultureInfo.CurrentCulture.CompareInfo;
string str = "منّم";
Console.WriteLine(info.IndexOf(str, "من", CompareOptions.Ordinal));
Output is 0.
DotNetFiddle if you want to try it yourself.
You should learn about the different methods that .Net uses to compare/match strings.
Best Practices for Using Strings in .NET
Some overloads with default parameters (those that search for a Char
in the string instance) perform an ordinal comparison, whereas others
(those that search for a string in the string instance) are
culture-sensitive. It is difficult to remember which method uses which
default value, and easy to confuse the overloads.
The section String Operations that Use the Invariant Culture gives a short explanation about combining characters.
I wrote VB.NET code like this:
d = Data.IndexOf("</a>", ("target='_top' class='ab1'>").Length() + s).
I want to write this in C#. When I wrote the above code in C#, it said there was an error with the Length keyword. How do I write the above code in C#?
Length is not a keyword in C# - it is either a property or an extension method on the object (like a string) that you are trying to manipulate.
So if it is a string you are using this will work:
myString.Length
(notice how the brackets are missing because it is a property).
You have an extra set of parentheses:
d = Data.IndexOf("</a>", "target='_top' class='ab1'>".Length + s)
Try that
Check this link out:
In it, you can easliy switch between C# to a VB good, to help you migrate:
http://msdn.microsoft.com/en-us/library/system.string.length.aspx#Y242
What is the VB statement expression Chr(0) equivalent in C#?
Thanks for any help.
The equivalent I believe is '\0'
I deleted the comment as I thought it is more appropriate to update in the post :)
sValue = vValue + Chr(0) 'As mentioned in your comment
can be written as
sValue += "\0";
You can use (char)0. Or '\0' of course. If you want to call a method, you can use Convert.ToChar(0).
The equivalent would be (char)0 . If you are looking for escape sequences and other characters you can use \n and likewise
sValue == vValue + Strings.Chr(0)
You can try this yourself using this website
How do I use the ToString method on an integer to display a 2-char
int i = 1; i.ToString() -> "01" instead of "1"
Thanks.
You can use i.ToString("D2") or i.ToString("00")
See Standard Numeric Format Strings and Custom Numeric Format Strings on Microsoft Docs for more details
This should do it:
String.Format("{0:00}",i);
Here's a link to an msdn article on using custom formatting strings:
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx
In order to ensure at least 2 digits are displayed use the "00" format string.
i.ToString("00");
Here is a handy reference guide for all of the different ways numeric strings can be formatted
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx
In C# 6 you could write:
var i = 1;
var stringI = $"{i:D2}";
$ - string interpolation
i.ToString("00") Take a look at this for more rules.
In any case you wanna check first if it's only 1 number, use Regular Expression:
Regex OneNumber = new Regex("^[0-9]$");
OneNumber.Replace(i.ToString(), "0" + i)
After I convert a decimal value salePr to string, using following code:
decimal salePr;
string salePrStr;
...
salePrStr = (salePr).ToString("0000.00");
'''
I'd like to get rid of leading zeros (in case result is <1000).
What is right and the best way to do this operation?
So why have you explicitly included them? Just use a format string of 0.00.
You could use trimstart to remove the leading zeros.
salePrStr = (salePr).ToString("0000.00").TrimStart(Convert.ToChar("0"));
It looks like you are trying to display currency, if you want to display it as currency, try salePrStr = String.Format("{0:C}", salePr) otherwise use the format 0.00
salePrStr = (salePr).ToString("###0.00");
The other answers are probably what you're looking for. If, for some reason, however, you actually want to keep the original strings (with leading zeroes), you can then write:
string salePrStr = salePr.ToString("0000.00");
string salePrStrShort = salePrStr.TrimStart('0');
Give this a try:
salePrStr = (salePr).ToString("N2");
That would make 1000.10 show as
1,000.10
and make 45.2305 show as
45.23
Just testing it in c#