What is the VB statement phrase "Chr(0)" equivalent in C#? - c#

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

Related

Using replace function with regex in C Sharp

Need some help on a problem please.
In fact I got a base64 string named "image" like that :
data:image/pjpeg;base64,iVBORw0KGgoAAAANSUhE...
I need to replace the part "data:image/pjpeg;base64," by "".
I try this way :
imageSrc = image.Replace("data:image/(png|jpg|gif|jpeg|pjpeg|x-png);base64,", "");
But it doesn't work.
Is somebody has an idea on that.
Thanks a lot
You should use the static Replace method on the Regex class.
imageSrc = Regex.Replace(image, "data:image/(png|jpg|gif|jpeg|pjpeg|x-png);base64,", "");
Well, for starters your code is doing String.Replace instead of Regex.Replace.
imageSrc = Regex.Replace(image, "data:image/(png|jpg|gif|jpeg|pjpeg|x-png);base64,", "");
But Regex is a rather heavy for this use case, why not just take everything after the comma?
imageSrc = image.SubString(image.IndexOf(",") + 1);
You are just using String.Replace, but you should use Regex.Replace for regular expressions.
But why not just use Substring?
imageSrc = image.Substring(image.IndexOf(',') + 1)
Since you know that your string is always starting with data:image/..., you don't need regular expressions at all.
Keep it simple and just take the substring after the first ,.
String.Replace() has no overload with regexp. Use Regex.Replace() instead.
There is a mistake in your regex, you must specify ?: for images alternatives and use Regex object, so :
Regex.Replace("data:image/(?:png|jpg|gif|jpeg|pjpeg|x-png);base64,", "");
it should work

What is equivalent to VB.NET's Length keyword in C#?

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

Correction in this simple regular expression

I am new to regular expressions and the one that i have written might be a very simple one but donot know where I am wrong.
#"^([a-zA-Z._]+)#([\d]+)"
This RE is for the following string:
somename#somenumber
Now i am trying to retrieve the somename and somenumber. This is what i did:
ac.name = m.Groups[0].Value;
ac.number = m.Groups[1].Value;
Here ac.name reads the complete string, and ac.number reads somenumber. Where am I wrong in ac.name?
i guess the regex is correct, the problem is, you get the ac.name not from group 1 but group(0), which is the whole string. try this:
ac.name = m.Groups[1].Value;
ac.number = m.Groups[2].Value;
This regex is correct. I think your mistake is in somewhere else. You seem to use C#. So, you should think about the regex usage in the language.
Looking to the code sample in MSDN, you need to use 1-based indexes while accessing Groups instead of zero-based (as also Kent suggested). So, use this:
String name = m.Groups[1].Value;
String number = m.Groups[2].Value;
use this regex (\w+)#(\d+([.,]\d+)?)
Groups[1] will be contain name
Groups[2] will be contain number
I think you should move the + into the capture group:
#"^([a-zA-Z._]+)#([\d]+)"
If this is C#, try without the ^
([a-zA-Z\._]+)#([\d]+)
I just tried it out and it groups properly
Update: escaped the .
If you want only one match (and hence the ^ in original expression), use .Match instead of .Matches method. See MSDN documentation on Regular Expression Classes.

Equivalent of Format of VB in C#

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

Frustrated trying to read a path from an argument in C#

I'm passing /file:c:\myfile.doc and I'm getting back "/file:c:\myfile.doc" instead of "C:\myfile.doc", could someone please advise where I am going wrong?
if (entry.ToUpper().IndexOf("FILE") != -1)
{
//override default log location
MyFileLocation = entry.Split(new char[] {'='})[1];
}
You are splitting on "=" instead of ":"
Try
if (entry.ToUpper().IndexOf("FILE:") == 0)
{
//override default log location
MyFileLocation location = entry.Split(new char[] {':'},2)[1];
}
The easiest way to do this is to just take a substring. Since you are reading this from the command line, the "/file:" portion will always be consistent.
entry.Substring(6);
This will return everything after the "/file:".
Not an answer as I think it's been answered well enough already, but as you stated that you're a beginner I thought that I would point out that:
entry.split(new char[]{':'});
can be:
entry.split(':');
This uses:
split(params char[] separator);
This can be deceiving for new C# programmers as the params keyword means that you can actually pass in 1 to many chars, as in:
entry.split(':','.',' ');
You could also just lop off the 'file:' part. It is clearly defined and will be constant so it isn't THAT bad. Not great, but not horrible.
Here is a good example of a command line argument parser.
The code you've posted would require the argument /file=c:\myfile.doc.
Either use that as the parameter or split on the colon (:) instead of equals (=).

Categories

Resources