Solved

C♯ method TimStart() method

Posted on 2016-09-29
18
20 Views
Last Modified: 2016-09-29
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.

strValue = "  0001234567890 "

strValue = strValue.Trim()
str = stringValue.TrimStart('0')
if(str.length> 10) {
...popup message
}
.....

Open in new window


Problem this:
method Trim()            => works
method TrimStart()    => error: Object doesn't support property or method 'TrimStart'

Any ideas?
Thank You
0
Comment
Question by:Darius
  • 8
  • 4
  • 3
  • +1
18 Comments
 

Author Comment

by:Darius
ID: 41822281
Acepted result should be:

strValue: '1234567890'
0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 41822286
strValue = "  0001234567890 "

strValue = strValue.Trim()
str = strValue.TrimStart('0')
if(str.length> 10) {
...popup message
}
.....
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41822325
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
0
 

Author Comment

by:Darius
ID: 41822333
Some grammer mistakes: strValue
0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 41822335
Yes that is the solution I gave above, correcting the typo by Darius
0
 

Author Comment

by:Darius
ID: 41822338
Thank Neil for corection...
0
 
LVL 32

Expert Comment

by:it_saige
ID: 41822339
What Neilsr said...  Proof of concept:
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();
		}
	}
}

Open in new window

Produces the following output -Capture.JPG
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();
		}
	}
}

Open in new window

Which gives us the same output.

-saige-
0
 

Author Comment

by:Darius
ID: 41822340
The problem is for trimStart() method
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41822343
Strange error message though unless stringValue is actually declared
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 37

Expert Comment

by:Neil Russell
ID: 41822344
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?
0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 41822351
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.
0
 

Author Comment

by:Darius
ID: 41822352
On my way home, check then...
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41822370
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.
0
 

Author Comment

by:Darius
ID: 41822619
Tried to separate Trim() and TrimStart('0') methods to see results.   TrimStart() gives error...
results below

error on debuging
0
 

Author Comment

by:Darius
ID: 41822625
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 = '';
        }

Open in new window

0
 
LVL 32

Expert Comment

by:it_saige
ID: 41822627
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-
0
 

Author Comment

by:Darius
ID: 41822644
change to 'trimStart' same issue...

else if (invoiceId.trimStart('0') == "00120") {
            ....
}
0
 
LVL 32

Accepted Solution

by:
it_saige earned 500 total points
ID: 41822652
I've been looking but I do not believe that javascript supports trimStart in the way you have characterized without using something like jQuery or another library that extends the language specification.  The best that I can find is the Microsoft specification that says trimStart is used to trim whitespace from the beginning of a string:  https://msdn.microsoft.com/en-us/library/bb310800.aspx

Looking in the Mozilla Javascript Reference, I cannot find trimStart specified: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String

I'll admit though, I am not an expert on javascript.

-saige-
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now