what happens if value is longer than fieldlength
-
hy everyone! what happens in cases like this:
string myinput = "12345678910"; myinput = myinput.toString({0:10}); // formats the string to a length of 10
Does this throw an exception or does it cut off all charecters > 10 ?? Because I want the string to be insert no matter if it is too long or not. If it is too long then this should be handled by a warning but the string should be insert without cutting off anything. So I wonder if this function tries to format it to length 10 but accepts strings which are longer as well, or if it doesn't. Because if it doesn't then i have to take care about this myself by checking, if the string is too long. Thanks. Stephan. -
hy everyone! what happens in cases like this:
string myinput = "12345678910"; myinput = myinput.toString({0:10}); // formats the string to a length of 10
Does this throw an exception or does it cut off all charecters > 10 ?? Because I want the string to be insert no matter if it is too long or not. If it is too long then this should be handled by a warning but the string should be insert without cutting off anything. So I wonder if this function tries to format it to length 10 but accepts strings which are longer as well, or if it doesn't. Because if it doesn't then i have to take care about this myself by checking, if the string is too long. Thanks. Stephan.Why write such a long post instead of just trying it to see ?
Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
-
hy everyone! what happens in cases like this:
string myinput = "12345678910"; myinput = myinput.toString({0:10}); // formats the string to a length of 10
Does this throw an exception or does it cut off all charecters > 10 ?? Because I want the string to be insert no matter if it is too long or not. If it is too long then this should be handled by a warning but the string should be insert without cutting off anything. So I wonder if this function tries to format it to length 10 but accepts strings which are longer as well, or if it doesn't. Because if it doesn't then i have to take care about this myself by checking, if the string is too long. Thanks. Stephan. -
hy everyone! what happens in cases like this:
string myinput = "12345678910"; myinput = myinput.toString({0:10}); // formats the string to a length of 10
Does this throw an exception or does it cut off all charecters > 10 ?? Because I want the string to be insert no matter if it is too long or not. If it is too long then this should be handled by a warning but the string should be insert without cutting off anything. So I wonder if this function tries to format it to length 10 but accepts strings which are longer as well, or if it doesn't. Because if it doesn't then i have to take care about this myself by checking, if the string is too long. Thanks. Stephan.I believe it will throw an exception..even sql server throws an exception that warns that string data is bieng truncated. your best solution would be to either limit what the user can type in, or check its length before you try to insert it, and either truncate it yourself, or tell the user that the input is invalid(best solution so the user doesnt get bad data in the db or where ever you're putting it)
-
stephan_007, What language is this in?, because it doesn't compile in C#. Regards, Gareth.
it's c# but i didn't copy it from my source, i just typed it into here. maybe i misstyped it. the reason why i posted it here is, maybe someone already did anything similar and could help me to keep it simple :) because it looks like i usually solve problems in a too difficult way which could be solved by default functions. that's why. i know i could try the two statements above in my own code, but as i already said, maybe someone knows a different way to format the string without raising an error ;)
-
it's c# but i didn't copy it from my source, i just typed it into here. maybe i misstyped it. the reason why i posted it here is, maybe someone already did anything similar and could help me to keep it simple :) because it looks like i usually solve problems in a too difficult way which could be solved by default functions. that's why. i know i could try the two statements above in my own code, but as i already said, maybe someone knows a different way to format the string without raising an error ;)
ups, i misstyped the second line! it should be
myinput = String.Format("{0,10}", myinput}
and i already found out, it ignores the formating, if the string is longer than defined in the formatsring. so i have to check it myself, by splitting off the relevant position in the format string to compare it to.modified on Monday, May 5, 2008 9:10 AM
-
I believe it will throw an exception..even sql server throws an exception that warns that string data is bieng truncated. your best solution would be to either limit what the user can type in, or check its length before you try to insert it, and either truncate it yourself, or tell the user that the input is invalid(best solution so the user doesnt get bad data in the db or where ever you're putting it)
the user wants this behaviour: if the string is smaller or equal the length defined in the format string, then just insert it, if it is longer than the length defined, then the programm should throw a worning, but insert the data anyway. i have to insert it into a stream. so if it is too long, then all other info is moved x characters to the right (x is the difference to the defined and the real length). well, because it just ignores the length if it is too long, i have to write a check myself to know, if i have to throw a warning or if the length is ok.
-
the user wants this behaviour: if the string is smaller or equal the length defined in the format string, then just insert it, if it is longer than the length defined, then the programm should throw a worning, but insert the data anyway. i have to insert it into a stream. so if it is too long, then all other info is moved x characters to the right (x is the difference to the defined and the real length). well, because it just ignores the length if it is too long, i have to write a check myself to know, if i have to throw a warning or if the length is ok.
you should probably try to explain to the user that just inserting the data even if its too long is a bad idea. you could just use the string.Length to find out how long it is, and use Remove to cut off any unwanted characters