Format string using multiple specifiers - c#

Is there a way to use Int32.ToString("<some string format specifier>") with using more than 1 specifiers?
Specifically, I want to format an int in Hexadecimal but force the string to be 8-bit long, by adding 0's in the empty spots.
For example, I want to parse the number 1234 in decimal to the string "000004D2".
The way I wanted to do this was by combining the specifiers "X" and "00000000", but I can't seem to find any examples of combining specifiers together. Do I need to create my own FormatProvider?
I need to do this because I am writing a viewer which displays an array of bytes which supports different packages and formats. For example, display the array as an array of 4-bytes integers in hexadecimal, or 2-bytes integers in signed display. Much like the Memory viewer in VS

For that specific example, you can just use "X8" as your format specifier. I don't know about the more general case - but if you have any other specific requirements, it's probably worth asking about those separately.

Related

How to format a string into a string with currency symbol and seperators

Numerical formatting of int, double, decimal can be simply achieved via using Standard Numerical Formatter for example assuming Culture is "en-GB":
int value = 1000;
Console.WriteLine(value.ToString("C0")); // Would output £1,000
However I am wondering if there is a simple way to format a string to something of the same effect as above. For example:
string amount = "£2000"; // Would want to format to "£2,000"
Is there a way to format this string so that the thousands separator is added in the correct position?
Given it's a string I don't think numerical formatting would work without converting the string to a numerical data type beforehand:
var result = Int32.Parse("£2000", NumberStyles.AllowCurrencySymbol, new CultureInfo("en-GB"));
Console.WriteLine(result.ToString("C0", new CultureInfo("en-GB"))); // Outputs £2,000
However its a bit verbose to convert string to int then back to string. Is there is a simpler way to do this given that the starting string has the currency symbol?
Given it's a string I don't think numerical formatting would work without converting the string to a numerical data type beforehand
Indeed.
Is there is a simpler way to do this given that the starting string has the currency symbol?
No. And I seriously doubt that such a feature would ever be added and/or welcomed by the developer community. A formal specification for such a feature would be a complexity nightmare.
Of course, in your particular case, if you are sure that your string always consists of "currency symbol + sequence of digits without comma or period", you could develop a string-based solution optimized for your use case (for example, a fancy regular expression). However, I think that your current solution is both readable and maintainable and you would do your future self a favor by keeping it that way. If you need it multiple times, extract it into a method.

How to search MongoDB for various phone number formats?

I'm attempting to search my MongoDB for string phone numbers that are all in different formats.
E.g. (323)704-3234, 3237043234, 323-704-3234,+1-323-704-3234,323.704.3234, etc.
Is there an operator or regex that I can use that MongoDB provides that allows you to find strings minus special characters?
For example in c#,
collection.Find(Query.Matches("PhoneNumber",(some regex, replace, or where)3237043234))
Sorry, but you are doing it wrong.
You can accept phone strings in any format (if you want so), but before storing them you need to convert them to some specific format. In your case the best will be just to convert it to number (smaller size to store, to create index).
Then one more time, when the user asks for a number - he can also provide it in any format, and before you search for it you will convert it to the number. (Do not forget to put index on phone number field) Being more user friendly, you can show the ouput in the same format the user asked it.
To change all your current phones, just iterate through the database and update each of the numbers.

Int.ToString format equivalent to String.Format("{0,9}" , int)

Is there any value of formatString such that:
intValue.ToString(formatString) == String.Format("{0,9}", intValue)
for all positive integer values up to 1 million?
In particular, the output must be 9 characters and padded with spaces at the start.
000000000 will give me the correct length but padded with zeros.
As to why I don't just use String.Format("{0,9}", intValue), I am trying to create a configuration file which a third party program will read and use to create an output file and I cannot change the code in this program. I know the program is written in C# and it is obvious from the current configuration file that it uses ToString().
You can't.
Composite format strings ({0:xxx}) are not supported by any of the ToString overloads, though the standard numeric format strings are supported by the methods that do use composite format strings.
Standard numeric format strings are also supported by the .NET Framework composite formatting feature, which is used by some Write and WriteLine methods of the Console and StreamWriter classes, the String.Format method, and the StringBuilder.AppendFormat method.

Convert Hebrew Letters into Equivalent Number

Other then hard coding this by hand I was wondering if there was a way that the.net framework would have this built in automaticaly, I know it can automatically convert hebrew dates into georgian dates but I need to convert hebrew numbers into georgian
IE א
= 1
ב
= 2
This goes into the hundreds. See here for more info.
Here is the approach that you should take:
Make Dictionary<char,int> that gives correspondence between each Hebrew letter and its numeric value
Parse the string one character at a time (best to do it right-to-left)
For each character, look up its value in the dictionary and add it to a running sum
Be sure to handle common scenarios for separating the hundreds-letters from the tens-letters (double-quotation mark) and separating the thousands-letters from the hundreds (single-quotation mark). For example, 5770 = ה'תש"ע.`. See the details in the link above for more on separations.
Edit: I just published a GitHub Repo that exposes functionality for converting Hebrew text to numbers, and numbers to their Hebrew letter equivalents.

How to convert between Progress Database Character[] datatype and C# strings?

Progress databases allow for a Character[x] datatype. How can I write to a particular x using C# and ODBC?
Please do not answer unless you understand the what Character[x] means... it is not a string (array of chars), it is an array of strings (which are arrays of chars).
I figured it out. The documentation I have refers to a datatype of character[20], format x(24). character[x] (where x is a number), is like an array of strings. Format x(24) means each string in the array can be 24 characters long.
Essentially characters[20], format x(24) is a string that is 20 * 24 characters long with each "array element" separated with a semi-colon (;).
If column "options" is defined as character[20], x(24) then to populate it with strings from 1 to 20, one would merely write:
row.options = "1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20";
To populate it with all empty strings write:
row.options = ";;;;;;;;;;;;;;;;;;;";
Format x(24) means each string in the array can be 24 characters long.
Not quite accurate, the format is a DISPLAY format, which is used by a lot of the Progress routines when displaying / printing / exporting this field. All character fields, whether they have an extent or not, are stored on the DB as variable length string. So you could easily have up to about 32K worth of data in each of your 20 extents.
The Progress ODBC Driver Guide doesn't seem to mention that type at all?

Categories