Create a custom string c# - c#

I have a numeric id of integer type. I want to add that integer type value to 00000 and concatenate it with string type variable.Assume my integer variable value is 1. After adding the variable it should look like 00001. And I want to concatenate with a string like "Added" and then convert whole thing to a string variable. like this "Added00001". As another example if my integer value is 111 it should look like "Added00111" at the end. Another example integer variable = 1234. Final string should be "Added01234". How to do it...? An example of how to do it.. or Any tutorial how to do such a thing would be really great. Thanks in advance.

You should use the short form format specifier of the ToString method. In the example below, D5 indicates that the integer 111 should be converted to a string with padded zeros to a length of 5 digits, e.g. 00111.
int i = 111;
string s = "Added";
var s = s + i.ToString("D5");
//s = "Added00111"

Related

Converting a string getting Wrong value

I write a simple code and need to get each number in a string as an Integer
But when I try to use Convert.ToInt32() it give me another value
Example:
string x="4567";
Console.WriteLine(x[0]);
the result will be 4 , but if i try to use Convert
Console.WriteLine(Convert.ToInt32(x[0]));
it Give me 52 !!
I try using int.TryParse() and its the same
According to the docs:
The ToInt32(Char) method returns a 32-bit signed integer that represents the UTF-16 encoded code unit of the value argument. If value is not a low surrogate or a high surrogate, this return value also represents the Unicode code point of value.
In order to get 4, you'd have to convert it to a string before converting to int32:
Console.WriteLine(Convert.ToInt32(x[0].ToString()));
you can alternatively do this way to loop through all characters in a string
foreach (char c in x)
{
Console.WriteLine(c);
}
Will Print 4,5,6,7
And as suggested in earlier answer before converting to integer make that as a sting so it doesn't return the ascii code
A char is internally a short integer of it's ASCII representation.
If you cast/convert (explicitly or implicitly) a char to int, you will get it's Ascii value. Example Convert.ToInt32('4') = 52
But when you print it to Console, you are using it's ToString() method implicitly, so you are actually printing the ASCII character '4'. Example: Console.WriteLine(x[0]) is equivalent to Console.WriteLine("4")
Try using an ASCII letter so you will notice the difference clearly:
Console.WriteLine((char)('a')); // a
Console.WriteLine((int)('a')); // 97
Now play with char math:
Console.WriteLine((char)('a'+5)); // f
Console.WriteLine((int)('a'+5)); // 102
Bottom line, just use int digit = int.Parse(x[0]);
Or the funny (but less secure) way: int digit = x[0] - '0';
x isnt x[0], x[0] is the first char, '4' which is 52.

how to convert a string into integer representation and change it back

I was wondering if there were any good methods to change a string into an integer representation and then be able to change the integer representation back to a string? There are a lot of ways to change a string to an integer representation by changing each character to ASCII, etc. but I was confused by how to change the ascii integer back into the fully formed string.
I am trying to represent my object of 3 strings into an integer so that I do not have to group by 3 string but just 1 integer.
For example an object of :
("hey",
"hello",
"goodbye")
would have an int representation of lets say 123423425 with some algorithm.
Then if possible I would like to be able convert that int from another algorithm back into
("hey",
"hello",
"goodbye")
Any help would be appreciated
int number = Int32.Parse("45");
string formatted = number.ToString()//add formatting if needed
You can go back and forth
Reference for format strings: http://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx
Convert char array to string
char[] charArray = {'1', '2', '3'};
String str = String.valueOf(charArray);

Round to 4 Decimal places not working?

Doing this:
double dblRateEvalResult = -0.52;
string strNewResult = dblRateEvalResult.ToString("000.####").TrimStart('-');
I want:
000.5200
I get:
000.52
What am I doing wrong?
You need 0 custom specifier instead of #
string str = dblRateEvalResult.ToString("000.0000").TrimStart('-');
(In your code you are trying to assign string to a double value, I guess this is a typo)
See: The "0" custom format specifier - Custom Numeric Format Strings
If the value that is being formatted has a digit in the position where
the zero appears in the format string, that digit is copied to the
result string; otherwise, a zero appears in the result string.

C# Convert Int64 to String using a cast

How do you convert an integer to a string? It works the other way around but not this way.
string message
Int64 message2;
message2 = (Int64)message[0];
If the message is "hello", the output is 104 as a number;
If I do
string message3 = (string)message2;
I get an error saying that you cant convert a long to a string. Why is this. The method .ToString() does not work because it converts just the number to a string so it will still show as "104". Same with Convert.ToString(). How do I make it say "hello" again from 104? In C++ it allows you to cast such methods but not in C#
message[0] gives first letter from string as char, so you're casting char to long, not string to long.
Try casting it back to char again and then concatenate all chars to get entire string.
ToString() is working exactly correctly. Your error is in the conversion to integer.
How exactly do you expect to store a string composed of non-numeric digits in a long? You might be interested in BitConverter, if you want to treat numbers as byte arrays.
If you want to convert a numeric ASCII code to a string, try
((char)value).ToString()
Another alternative approach is using ASCII.GetBytes method as below
string msg1 ="hello";
byte[] ba = System.Text.Encoding.ASCII.GetBytes(msg1);
//ba[0] = 104
//ba[1] = 101
//ba[2] = 108
//ba[3] = 108
//ba[4] = 111
string msg2 =System.Text.Encoding.ASCII.GetString(ba);
//msg2 = "hello"
Try this method:
string message3 = char.ConvertFromUtf32(message2);
104 is the value of "h" not "hello".
There is no integer representation of a string, only of a char. Therefore, as stated by others, 104 is not the value of "hello" (a string) but of 'h' (a char) (see the ASCII chart here).
I can't entirely think of why you'd want to convert a string to an int-array and then back into a string, but the way to do it would be to run through the string and get the int-value of each character and then reconvert the int-values into char-values and concatenate each of them. So something like
string str = "hello"
List<int> N = new List<int>();
//this creates the list of int-values
for(int i=0;i<str.Count;i++)
N.Add((int)str[i]);
//and this joins it all back into a string
string newString = "";
for(int i=0;i<str.Count;i++)
newString += (char)N[i];

Convert enum to string digit

I want to convert enum to string with three digits in C#.
Like this:
enum test : short
{
asdf, qwer, zxcv
}
test t = test.qwer;
string result = t.ToString("%03D") // Is this right?
I want to print 001
In a normal enumeration you can just cast as an integer, then call "ToString" with a format specified. In your case that should look like:
test t = test.qwer;
string result = ((int)t).ToString("000");
That should do it! Good luck.

Categories

Resources