How to format a string through String.Format - c#

Numeric
string a = String.Format("{0:#/#}",12)
output:1/2
But How to Convert it for string Suppose
String b=String.Format("{0:###-#}","test")
Output : test
Expected Output: tes-t

You can't format a string through string.Format for the reason that it is already a string.
If you want to manipulate the string, use the methods defined on the string class.

I think you can split the string to its characters and then use format
string testString = "test";
string formattedString = string.Format("{0}{1}{2}-{3}", testString.Select(c => c.ToString()).ToArray());

Related

How to perform currency formatting with string interpolation in c#?

Is it possible to perform string formatting using c#'s string interpolation with a specific CultureInfo?
For example if you are in a en-US System and want the number to be formatted with pt-PT how to I achieve that with string interpolation?
You can use CultureInfo.CreateSpecificCulture for specifying the culture and the C formatter for converting the value into a string with a currency.
double amount = 50.5;
string str = $"{amount.ToString("C", CultureInfo.CreateSpecificCulture("pt-PT"))}";
Interpolated strings can be converted to FormattableStrings, which can be formatted with a IFormatProvider:
var formattableString = (FormattableString)$"{1.1:C}";
var result = formattableString.ToString(new CultureInfo("pt-PT"));
// "1,10 €"

How do I convert a string to a verbatim string using its identifier

For one method I have the parameters take a string, but I need that string to become a verbatim string once in the method.
Here is an example of what I mean.
//the string I have
string test = "c:\\documents\\testfile\\test.txt"
//the string I want
string converted = #"c:\\documents\\testfile\\test.txt"
how do I go about converting this using the test identifier?
I have tried:
string test = "c:\\documents\\testfile\\test.txt"
string converted = #test;
string converted = #+test;
string converted = #""+test;
Is there a way I can do this? Thanks
You can't use the verbatim identifier like you are trying to. You'll have to do something along the lines of:
string test = "c:\\documents\\testfile\\test.txt";
string converted = test.Replace(#"\", #"\\");

How to format a string into a certain pattern?

I have a string of telephone number. The format I wanted to use is like this
(044) 463-2256
How can I format my string like this?
string tel = "0444632256";
var match = Regex.Replace(tel, #"");
I tried using Regex.Replace but I don't know what the format that I will put in.
If the Length of the string is always constant(10) , then wecan use the below method to format
string tel = "0444632256";
var format = $"({tel.Substring(0, 3)}) {tel.Substring(3, 3)}-{tel.Substring(6, 4)}";
Console.WriteLine(format);
Try this code:
string tel = "0444632256";
tel=String.Format("{0:(###) ###-####}", Int64.Parse(tel));
Output:
(44) 463-2256
You can see below answer:
Convert.ToInt64(tel).ToString("###-###-#### ####")
How to format a string as a telephone number in C#

How to make string of specified length from another string c#

For example I have strings like:
"5", "8", "14", "260"
and I want to get result like:
"ST00000005", "ST00000008", "ST00000014", "ST00000260"
result string length is 10 chars. How can I do it?
I would store it as int not as string. Then you can use ToString with the appropriate format specifier D8. That has f.e. the advantage that you can increase the number:
int number = 5;
string result = String.Format("ST{0}", number.ToString("D8"));
or without ToString but only String.Format:
string result = String.Format("ST{0:D8}", number);
Read: Standard Numeric Format Strings especially Decimal ("D") Format Specifier
If you need to convert a string to int use int.Parse or int.TryParse.
For the sake of completeness, if you have to use strings use String.PadLeft(8, '0'):
string numStr = "5";
String result = String.Format("ST{0}", numStr.PadLeft(8, '0'));
int number = 5; // put the number here
string result = $"ST{number:0000000#}";
// Or:
string result = $"ST{number:D8}";
This does exactly what you want.
EDIT: Keep in mind that this is only possible in C#6
You can do this like
string s = "215";
s = s.PadLeft(8, '0').PadLeft(9,'T').PadLeft(10,'S');
Use string.Format() together with a custom format string.

show string in a desire format

I want to show my string in a special format.
For example I have a string like this s = "00012345"; and want to show it in this format 000(123-45)
I use this code for it:
label1.text = string.format("{0,###(###-##)}",s);
But just see this for result: 00012345.
What string formatting should I be using?
As mentioned you can't format an already existing string. First you need to convert the string to an int:
int num = Int32.TryParse(s);
Then you can do the formatting whilst converting it back into a string:
label1.text = num.ToString("000(000-00)");
string s = "00012345";
string s1 = s.Substring(0,3);
string s2 = s.Substring(3,3);
string s3 = s.Substring(6);
s = string.Format("{0}({1}-{2})", s1,s2,s3);

Categories