Solved

How do you trim the white space from between two words in a txtbox.

Posted on 2008-10-02
10
937 Views
Last Modified: 2013-12-17
I am trying to trim the white space form between two words in a txt box.  The reason for this is i want to run a query to check the db for a match.  What happens now is lets say i type mike Jones in the textbox and run my query it find it.  but if theres any more than one space between mike and Jones my query returns as no match.

Thanks for any help
0
Comment
Question by:bmanmike39
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
10 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22629056
In your case, you may be able to do something like this:
{http://msdn.microsoft.com/en-us/library/fk49wtc1.aspx}
textbox1.Replace(" ", "%");

Open in new window

0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22629067
% == wildcard for MS SQL Server

If using a different database, use the wildcard you would normally use in a LIKE statement, e.g. "*".
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 22629544
I can give you some generic function to remove extra white spaces from text. Probably it bwtter works for big text but also cam be used in your case. It handles spaces and tabs, i.e all white space characters. BTW performance for big text is better than for regular expressions.
See the snippet:
        private string TrimInnerSpaces(string input)
        {
            char[] inputArray = input.ToCharArray();
            StringBuilder sb = new StringBuilder(inputArray.Length);
            int whiteSpaceCounter = 0;
            char currentChar;
            for (int i = 0; i < inputArray.Length; i++)
            {
                currentChar = inputArray[i];
                if (char.IsWhiteSpace(currentChar))
                {
                    if (whiteSpaceCounter > 0)
                    {
                        // do nothing;
                    }
                    else
                    {
                        whiteSpaceCounter += 1;
                        sb.Append(' ');// append one space
                    }
                }
                else
                {
                   whiteSpaceCounter = 0;
                   sb.Append(inputArray[i]);
                }
            }
 
            return sb.ToString();
        }

Open in new window

0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22629940
anarki_jimbel, are you indicating my String.Replace won't work?  Or are you just offering an alternative?
0
 
LVL 3

Expert Comment

by:trunghieubkit
ID: 22631099
Using the snippet below,
It's yours!

It'll work well with either Tab or Space between the words

Example:
"               Hieu                           Dung          "  =>  "Hieu Dung"
Or
"               Hieu                           Dung         Hien "  =>  "Hieu Dung Hien"

string Elimate(string astrOriginal)
{
    string lstrResult = "";
    string[] lstrSplit = { " ", "\t" }; //Both Space and Tab
    string[] lstrSubs = astrOriginal.Split(lstrSplit, StringSplitOptions.RemoveEmptyEntries);
 
    foreach (string lstrSub in lstrSubs)
    {
        lstrResult += " " + lstrSub;
    }
 
    return lstrResult.Trim();
}

Open in new window

0
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 250 total points
ID: 22645851
mwvisa1
Really I'm just proposing an alternative. But I am not sure your code will work for a string (text), we are not dealing with a DB here.

trunghieubkit:
you code will work but it's highly inefficient for a big text (just in case).
Better change to (see the snippet):
string Elimate(string astrOriginal)
{
    string lstrResult = "";
    string[] lstrSplit = { " ", "\t" }; //Both Space and Tab
    string[] lstrSubs = astrOriginal.Split(lstrSplit, StringSplitOptions.RemoveEmptyEntries);
 
    lstrResult = string.Join(" ", lstrSubs);
 
    return lstrResult;
}

Open in new window

0
 
LVL 60

Assisted Solution

by:Kevin Cross
Kevin Cross earned 250 total points
ID: 22645868
@anarki,

I was just curious.

As far as DB, see this from question:
>> The reason for this is i want to run a query to check the db for a match.

My solution was geared around changing the spaces in the text from textbox to "%" so that when passed into the SQL query it will look something like this:

columnname LIKE 'John%Smith'

Thanks for asking as that may not have been clear to the asker.
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 22646329
Agree, probably it's the best solution for the case.
0

Featured Post

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

688 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