473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

C#: How to find an empty string

126 100+
Hello Guys,

I have a textbox called "NameTextBox" and "Submit" button in my "index.aspx" page.

I need to check whether user entered name in the textbox before clicking submit button.

And, this is what I did:

Expand|Select|Wrap|Line Numbers
  1. string name = NameTextBox.Text.ToString();
  2. if (name.Length == 0)
  3. {
  4.    return;
  5. }
But if the user enters spaces in the textbox, I am unable to check the string as empty.

Please let me know how to solve this issue.
Thanks.
Nov 27 '08 #1
3 3667
JFKJr
126 100+
Solved the problem by using Trim() Method, but ran into another problem.

Using Trim() method, I am removing all spaces in a string including spaces between words. But, I only want to remove spaces at the beginning of the string. How can I do this?

Any help/idea will be greatly appreciated.
Thanks.
Nov 27 '08 #2
jg007
283 100+
try Trimstart -

Example_String.TrimStart(' ')
Nov 27 '08 #3
tlhintoq
3,525 Expert 2GB
I've kinda followed this format in a lot of places

Expand|Select|Wrap|Line Numbers
  1.         private void testing()
  2.         {
  3.             string szFirst = tbFirstName.Text;
  4.             bob.Trim(); // By definition "removes all trailing and leading whitespace characters"
  5.             if (string.IsNullOrEmpty(szFirst )) { /* do nothing */ }
  6.             else
  7.             {
  8.                 // do somthing here
  9.             }
  10.         }
  11.  
If you only trim the start, you don't catch trailing whitespace.
If you only trim ' ', you only trim out the ASCII 32 {space] character, not all forms of whitespace.

As for trimming the space between first and last name...
Use different boxes then combine them if you need a combined field.
In the real world not everyone is "John Smith". You have to take into consideration people with hyphenated to two part last names... someone who under "name" would enter first, middle, last... or people who might enter "last, first i."... So guide them with a well thought-out form that doesn't leave room for interpretation. Besides, you're going to want their last name as a separate field at some point so you can sort data, or their first name by it self for a friendly salutation.
Nov 27 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Jaime Wyant | last post by:
Will someone explain this to me? >>> "test".find("") 0 Why is the empty string found at position 0? Thanks! jw
4
by: Greg Baker | last post by:
I don't know what standard protocol is in this newsgroup. Am I allowed to post code and ask for help? I hope so.. :) Here's my problem: I am trying problem 127 of the valladolid online...
22
by: mail.zhf | last post by:
if have two char use equality, out first. example: char str = "gbdfssdffss"; out is: f.
4
by: Pieter | last post by:
Hi, I'm trying to get the whole UNC path of a file, and I would prefer to have it via a .NET-way (I'm using VB.NET 2005). So what I am basicly looking for is: X:\MyDirectory\MyFile.doc -> ...
4
by: Mike Charney | last post by:
What is the easiest and simplest way to find out if a table exists in an Access Data Project? (SQL Svr 2000 Standard using MS-Access 2003 for connection.) Mike Charney m charney at dunlap...
26
by: anonieko | last post by:
In the past I always used "" everywhere for empty string in my code without a problem. Now, do you think I should use String.Empty instead of "" (at all times) ? Let me know your thoughts.
2
by: karinmorena | last post by:
I'm having 4 errors, I'm very new at this and I would appreciate your input. The error I get is: Week5MortgageGUI.java:151:cannot find symbol symbol: method allInterest(double,double,double)...
2
by: SM | last post by:
Ok, this must be simple but the more i search the more i don't find. It's about SimpleXML and PHP. How to find non-existing nodes or nodes with no text My XML file looks a little bit like this:...
3
by: SM | last post by:
I think that simpleXML is the best thing i've seen in PHP5. It's so simple (d'ho!) and easy to use when it comes to reading a simple XML file. The only thing i'm not sure how to achieve is...
9
by: WebArchitect | last post by:
I have a text file. My text file contains lines with double quotes around it. I trying to code if a line has double quotes around it Then make it bold My code is below and it is not working....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.