string formate
-
Hi all, I need to format string and having some problems. when user enter into a textbox i.e "12345678". I need to format this input into this. "12 345 678". I am doing this way but not working
string p = TextBox.Text.Trim();
string.Format("{0},{1},{2}", p.Substring(0,2), p.Substring(2,4), p.Substring(5,7)); -
Hi all, I need to format string and having some problems. when user enter into a textbox i.e "12345678". I need to format this input into this. "12 345 678". I am doing this way but not working
string p = TextBox.Text.Trim();
string.Format("{0},{1},{2}", p.Substring(0,2), p.Substring(2,4), p.Substring(5,7));You should probably remove the comma's from your Format string. The rest is just about adding up your substring index and length values correctly.
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost
-
Hi all, I need to format string and having some problems. when user enter into a textbox i.e "12345678". I need to format this input into this. "12 345 678". I am doing this way but not working
string p = TextBox.Text.Trim();
string.Format("{0},{1},{2}", p.Substring(0,2), p.Substring(2,4), p.Substring(5,7)); -
Hi all, I need to format string and having some problems. when user enter into a textbox i.e "12345678". I need to format this input into this. "12 345 678". I am doing this way but not working
string p = TextBox.Text.Trim();
string.Format("{0},{1},{2}", p.Substring(0,2), p.Substring(2,4), p.Substring(5,7));Try this:
string p = TextBox.Text.Trim();
p = string.Format("{0} {1} {2}", p.Substring(0, 2), p.Substring(2, 3), p.Substring(5, 3));
// If you are using Windows Forms, this will show the output. You will do it differently if you are using ASP.NET.
MessageBox.Show(p);Here is what was wrong with your code:
- Your sample output contains spaces, but your format strong contains commas.
- string.Format returns the modified string, yet you were not assigning the return value to anything.
- string.Substring has 2 parameters. The second prameter is the string length, not a second index. So where you used 4 and 7, you should have used 3.
-
mabrahao wrote:
String.Format is uppercase in the S
Actually, both
string
andString
will work. -
Hi all, I need to format string and having some problems. when user enter into a textbox i.e "12345678". I need to format this input into this. "12 345 678". I am doing this way but not working
string p = TextBox.Text.Trim();
string.Format("{0},{1},{2}", p.Substring(0,2), p.Substring(2,4), p.Substring(5,7));One thing you should know - the second parameter in Substring is not the position to end on, it's the length of the substring to extract. It goes
Substring(index, length);
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
Try this:
string p = TextBox.Text.Trim();
p = string.Format("{0} {1} {2}", p.Substring(0, 2), p.Substring(2, 3), p.Substring(5, 3));
// If you are using Windows Forms, this will show the output. You will do it differently if you are using ASP.NET.
MessageBox.Show(p);Here is what was wrong with your code:
- Your sample output contains spaces, but your format strong contains commas.
- string.Format returns the modified string, yet you were not assigning the return value to anything.
- string.Substring has 2 parameters. The second prameter is the string length, not a second index. So where you used 4 and 7, you should have used 3.
-
One thing you should know - the second parameter in Substring is not the position to end on, it's the length of the substring to extract. It goes
Substring(index, length);
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
Hi all, I need to format string and having some problems. when user enter into a textbox i.e "12345678". I need to format this input into this. "12 345 678". I am doing this way but not working
string p = TextBox.Text.Trim();
string.Format("{0},{1},{2}", p.Substring(0,2), p.Substring(2,4), p.Substring(5,7));You should NOT do this with string manipulations; it would go completely wrong if the user types strange things, such as the intended number prefixed with a lot of zeroes. The proper way to do this consists of two steps: 1. parse the user input, i.e. turn his characters into an actual number. The simplest way would be:
int val;
bool OK=int.TryParse(myTextBox.Text, out val);Whatever the user does wrong (e.g. typing letters, or providing no input at all) will result in OK being false; if the input is acceptable, OK will be true and val will contain its value. 2. format the number in the way you want it. A simple way to get two spaces in a number assumed to require 8 digits is:
string s=string.Format("{0:## ### ###.##}", val);
Assuming
val=12345678
the result will be12 345 678
:)Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.3 -
You should NOT do this with string manipulations; it would go completely wrong if the user types strange things, such as the intended number prefixed with a lot of zeroes. The proper way to do this consists of two steps: 1. parse the user input, i.e. turn his characters into an actual number. The simplest way would be:
int val;
bool OK=int.TryParse(myTextBox.Text, out val);Whatever the user does wrong (e.g. typing letters, or providing no input at all) will result in OK being false; if the input is acceptable, OK will be true and val will contain its value. 2. format the number in the way you want it. A simple way to get two spaces in a number assumed to require 8 digits is:
string s=string.Format("{0:## ### ###.##}", val);
Assuming
val=12345678
the result will be12 345 678
:)Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.3I always like your answers.
-
You should NOT do this with string manipulations; it would go completely wrong if the user types strange things, such as the intended number prefixed with a lot of zeroes. The proper way to do this consists of two steps: 1. parse the user input, i.e. turn his characters into an actual number. The simplest way would be:
int val;
bool OK=int.TryParse(myTextBox.Text, out val);Whatever the user does wrong (e.g. typing letters, or providing no input at all) will result in OK being false; if the input is acceptable, OK will be true and val will contain its value. 2. format the number in the way you want it. A simple way to get two spaces in a number assumed to require 8 digits is:
string s=string.Format("{0:## ### ###.##}", val);
Assuming
val=12345678
the result will be12 345 678
:)Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.3Could be also 1. If the currect culture uses ' ' as a group separator:
string.Format("{0:n0}", val)
2. If it doesn't:
var provider = (CultureInfo) CultureInfo.CurrentCulture.Clone();
provider.NumberFormat.NumberGroupSeparator = " ";
string.Format(provider, "{0:n0}", val);Greetings - Jacek
-
Could be also 1. If the currect culture uses ' ' as a group separator:
string.Format("{0:n0}", val)
2. If it doesn't:
var provider = (CultureInfo) CultureInfo.CurrentCulture.Clone();
provider.NumberFormat.NumberGroupSeparator = " ";
string.Format(provider, "{0:n0}", val);Greetings - Jacek
yes, there are alternatives for each of both steps. Having the two steps was the essence in my message. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.4 -
yes, there are alternatives for each of both steps. Having the two steps was the essence in my message. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.4 -
To be precise: My (1) and (2) were alternatives to your (2). They both assumes that your (1) was already done.
Greetings - Jacek
Of course; and that is how I understood it. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.4