C# equivalent of Python repr() [duplicate] - c#

This question already has answers here:
Can I convert a C# string value to an escaped string literal?
(16 answers)
Closed 9 years ago.
Is there a C# method like Python's repr() to get the true representation of the object? Suppose we have:
string identifier = "22\n44";
Console.WriteLine(identifier);
This would return
22
44
Is there a way to get
"22\n44"
In Python this is easy. We can just do repr("22\n44").
I thought of this question because I was trying to convert "2244" to '2244' using
var identifier = "2244";
identifier = identifier.Replace("\"", "'");
Console.WriteLine(identifier);
The output is just 2244, because double quotes are for our purpose. But in my case, I did this to get what I wanted:
identifier = string.Format("'{0}'", identifier);
because initially the database was receiving the query as IN ("2244") instead of IN ('2244') and was throwing Invalid Number error.

string identifier = "22\n44";
Console.WriteLine("\"{0}\"", identifier.Replace("\n", #"\n"));

Related

c# convert string to array on char(253) deliminator [duplicate]

This question already has answers here:
How do i split a String into multiple values?
(5 answers)
Closed 3 years ago.
Not entirely sure the following code is going to help many people, but here goes
try
{
uvConnect = UniObjects.OpenSession(serverId, sUser, sPass, sAcct, "uvcs");
// Open Movie File
UniFile uvFile = uvConnect.CreateUniFile("MOVIES");
UniDynArray movieRec = uvFile.Read(txtMovieId.Text);
string sMovieData = movieRec.StringValue;
MessageBox.Show(sMovieData);
}
sMovieData contains a single string of the entire record retrieve from MOVIES file, each field is deliminated by a char(253) character in the database I am using.
Is there a function/method/etc to convert the string to an array using char(253) as a value deliminator
Something like this should work:
string[] fields = sMovieData.Split((char)253);
Try this... string[] arrayValues = "stringToConvertToArray".Split((char)253);

c# - How can i format a string in c# [duplicate]

This question already has answers here:
Add zero-padding to a string
(6 answers)
Closed 4 years ago.
I have a very simple Question to ask.
I have a string like:
string str="89";
I want to format my string as follow :
str="000089";
How can i achieve this?
Assuming the 89 is actually coming from another variable, then simply:
int i = 89;
var str = i.ToString("000000");
Here the 0 in the ToString() is a "zero placeholder" as a custom format specifier; see https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings
If you have a string (not int) as the initial value and thus you want to pad it up to length 6, try PadLeft:
string str = "89";
str = str.PadLeft(6, '0');
If you want the input to be a string you'll have to parse it before you output it
int.Parse(str).ToString("000000")

Convert string to double, [duplicate]

This question already has answers here:
How do I parse a string with a decimal point to a double?
(19 answers)
Closed 5 years ago.
I have a string array which contains 4 elements. Which looks like this.
How ever, when trying to do this:
Vector newVector = new Vector(
(float)Convert.ToDouble(words[1]),
(float)Convert.ToDouble(words[2]));
I get the following error:
'Input string was not in a correct format.'
And that is because it's because the value uses a '.' but if I manually change the array to use a ',' it works.
How can I easiest replace all '.' with ','.
Use
//(float)Convert.ToDouble(words[1]),
(float)Convert.ToDouble(words[1], CultureInfo.InvariantCulture),
Try this...
Vector newVector = new Vector(
(float)Convert.ToDouble(words[1], CultureInfo.GetCultureInfo("en-US").NumberFormat),
(float)Convert.ToDouble(words[2], CultureInfo.GetCultureInfo("en-US").NumberFormat));

Hex long number to string that looks the same [duplicate]

This question already has answers here:
Convert a number into the hex value in .NET
(2 answers)
Closed 6 years ago.
I've got a simple question i have got a long value presented in this way
long value = 0x001f0347
Now is there's a way to convert it to string that looks the same:
string value = "0x001f0347";
I have tried some converters but no luck.
Try formatting ("x8" format string - 8 hexadecimal digits):
long value = 0x001f0347;
string result = "0x" + value.ToString("x8");
If you prefer Convert then convert using toBase == 16 and pad left up to 8 symbols
string result = "0x" + Convert.ToString(value, 16).PadLeft(8, '0');

Invalid string in C# [duplicate]

This question already has answers here:
What's the # in front of a string in C#?
(9 answers)
Closed 7 years ago.
How come this string is valid to open with VLC via a Process:
string fileToPlay = #"C:\Videos\Movies\Movie title.avi";
But this one isn't:
string fileToPlay = #myMovie;
Where the value of the variable myMovie is
"C:\Videos\Movies\Movie title.avi"
Process.Start(vlcPath, fileToPlay );
The problem is that you can only use the # character when placed against string literals like this:
string path = #"c:\temp";
It can be used when placed against a string variable, as you have done, but it has a different meaning. In that case, it is used when you choose an identifier which matches a C# keyword, like this:
string #class = "hello";
You can read more about it here: https://msdn.microsoft.com/en-us/library/aa691090%28v=vs.71%29.aspx

Categories