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);
Related
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(#"\", #"\\");
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#
I want to seperate a number with commas. I have tried many ways to do it. But didn't work.
Its already converted to a string and now i want to format the "tot".
GetData getData = new GetData();
string tot = Convert.ToString(getData.Total_Extra(month));
string totVal = (tot).ToString("N",new CultureInfo("en-US"));
LB2.Text = tot.ToString();
You can convert your tot string to a numeric value and then use string.Format to get the desired format:
string tot = "19950000";
string output = string.Format("{0:n2}", Convert.ToInt32(tot));
Debug.WriteLine(output); //19,950,000.00 on my machine
or alternatively:
string output2 = Convert.ToInt32(tot).ToString("n2");
These are both culture specific, so might display differently on different users machines (Indian culture will display 1,99,50,000.00 for example).
If you want to force the three digit comma grouping then you can specify a culture to use:
string output2 = Convert.ToInt32(tot).ToString("n2", CultureInfo.CreateSpecificCulture("en-GB"));
//19,950,000.00 on any machine
It sounds like your tot may not be a numeric value, so you should check this before trying to format it:
string tot = "19950000";
int totInt;
if (Int32.TryParse(tot, out totInt))
{
string output = totInt.ToString("n2", CultureInfo.CreateSpecificCulture("en-GB"));
MessageBox.Show(output);
}
else
{
MessageBox.Show("tot could not be parsed to an Int32");
}
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());
Can I use numbers while using String data type?
Sure you can, and if you want to use them as numbers you can parse the string. E.g. for an integer:
string numberAsString = "42";
int numberFromString;
if (int.TryParse(numberAsString, out numberFromString))
{
// number successfully parsed from string
}
TryParse will return a bool telling if the parsing were successful. You can also parse directly if you know the string contains a number - using Parse. This will throw if the string can't be parsed.
int number = int.Parse("42");
You can have numbers in a string.
string s = "123";
..but + will concatenate strings:
string s = "123";
string other = "4";
Debug.Assert(s + other != "127");
Debug.Assert(s + other == "1234");
Numbers can be easily represented in a string:
string str = "10";
string str = "01";
string str = 9.ToString();
However, these are strings and cannot be used as numbers directly, you can't use arithmetic operations on them and expect it to work:
"10" + "10"; // Becomes "1010"
"10" / "10"; // Will not compile
You can easily store numbers as a string:
string foo = "123";
but that only helps you if you actually want numbers in a string. For arithmetic purposes, use a number. If you need to display that later, us a format string.
String number1 = "123456";
keep in mind. using that number for arithmatic purpose, you have to convert that string into proper type like
int number1Converted = Int32.Parse(number1);
int.TryParse(number1 , out number1Converted );
for double
double doubleResult = 0.0;
double.TryParse("123.00", out doubleResult);