Darius
asked on
C♯ method TimStart() method
Hi guys,
I working on pretty old project from 2010 (.net framework 4)
I need to validate entries of string value:
1. Take it off leading and trailing empty space from input string.
2. Leading 'zeros' from input string and after length should be not greater then 10.
Problem this:
method Trim() => works
method TrimStart() => error: Object doesn't support property or method 'TrimStart'
Any ideas?
Thank You
I working on pretty old project from 2010 (.net framework 4)
I need to validate entries of string value:
1. Take it off leading and trailing empty space from input string.
2. Leading 'zeros' from input string and after length should be not greater then 10.
strValue = " 0001234567890 "
strValue = strValue.Trim()
str = stringValue.TrimStart('0')
if(str.length> 10) {
...popup message
}
.....
Problem this:
method Trim() => works
method TrimStart() => error: Object doesn't support property or method 'TrimStart'
Any ideas?
Thank You
strValue = " 0001234567890 "
strValue = strValue.Trim()
str = strValue.TrimStart('0')
if(str.length> 10) {
...popup message
}
.....
strValue = strValue.Trim()
str = strValue.TrimStart('0')
if(str.length> 10) {
...popup message
}
.....
TrimStart is a member of the string class
strValue = " 0001234567890 "
strValue = strValue.Trim()
str = stringValue.TrimStart('0') //What is stringValue - or do you mean strValue
strValue = " 0001234567890 "
strValue = strValue.Trim()
str = stringValue.TrimStart('0') //What is stringValue - or do you mean strValue
ASKER
Some grammer mistakes: strValue
Yes that is the solution I gave above, correcting the typo by Darius
ASKER
Thank Neil for corection...
What Neilsr said... Proof of concept:
We could also simplify your code somewhat by chaining method calls:
-saige-
using System;
namespace EE_Q28973366
{
class Program
{
static void Main(string[] args)
{
string value = " 0001234567890";
// Removes all leading and trailing white-space characters from the string.
value = value.Trim();
// Removes all leading occurances of the specified character, 0.
string result = value.TrimStart('0');
Console.WriteLine("Resulting string - {0}; {1}.", result, result.Length > 10 ? "has more than 10 characters" : string.Format("has exactly {0} characters", result.Length));
Console.ReadLine();
}
}
}
Produces the following output -We could also simplify your code somewhat by chaining method calls:
using System;
namespace EE_Q28973366
{
class Program
{
static void Main(string[] args)
{
string value = " 0001234567890";
// Removes all leading and trailing white-space characters from the string and then
// removes all leading occurances of the specified character, 0.
string result = value.Trim().TrimStart('0');
Console.WriteLine("Resulting string - {0}; {1}.", result, result.Length > 10 ? "has more than 10 characters" : string.Format("has exactly {0} characters", result.Length));
Console.ReadLine();
}
}
}
Which gives us the same output.-saige-
ASKER
The problem is for trimStart() method
Strange error message though unless stringValue is actually declared
TrimStart method works fine in C# .NET4
Your error was in the use of a wrong variable name. Have you pasted my corrected code and run it?
Your error was in the use of a wrong variable name. Have you pasted my corrected code and run it?
And don't forget that "The problem is for trimStart() method" is NOT the same as
"The problem is for TrimStart() method"
Make sure that all capitalization is correct both in your code and in code submitted in the question please or we could be here a while.
"The problem is for TrimStart() method"
Make sure that all capitalization is correct both in your code and in code submitted in the question please or we could be here a while.
ASKER
On my way home, check then...
As I mentioned earlier that error message would not be produced from a simple typo between strValue and stringValue, it would need stringValue to be declared and also not to be a string type of variable.
This sounds like you have given a piece of air code (not copied/pasted actual code). If that is correct please do NOT do that in future. I have seen in multiple questions with edited/invented code being used where the original error has been accidentally corrected and/or another error introduced.
This sounds like you have given a piece of air code (not copied/pasted actual code). If that is correct please do NOT do that in future. I have seen in multiple questions with edited/invented code being used where the original error has been accidentally corrected and/or another error introduced.
ASKER
ASKER
else if (invoiceId.Trim().lenght > 15) {
warn = "!" + "Invoice ID has more then 15 characters.\n";
//in this case the page is not reloaded, so the button has to be enabled and the cursor back to 'default'
document.body.style.cursor = 'default';
document.getElementById("btnSend").onclick = '';
}
else if (invoiceId.TrimStart('0').lenght > 10) {
warn = "!" + "Invoice ID has more then 10 characters.\n";
//in this case the page is not reloaded, so the button has to be enabled and the cursor back to 'default'
document.body.style.cursor = 'default';
document.getElementById("btnSend").onclick = '';
}
I was afraid of this based upon the samples you provided. This is javascript, not C# (updated tags to reflect).
Not to split hairs but length is mispelled. Also as convention the javascript method is trimStart not TrimStart.
-saige-
Not to split hairs but length is mispelled. Also as convention the javascript method is trimStart not TrimStart.
-saige-
ASKER
change to 'trimStart' same issue...
else if (invoiceId.trimStart('0') == "00120") {
....
}
else if (invoiceId.trimStart('0') == "00120") {
....
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
strValue: '1234567890'