How do I remove extra spaces in a string?

Source: Internet
Author: User
Q:

I know that you can use the split command to explain the following sentence:VBScript is fun!Separated into an array of single words. But what if I have a sentence similar to the following:VBScript is fun!I cannot use spaces as separators because there may be many spaces. In addition, I cannot use a specific number of spaces as separators, because the number of spaces may be different. What are your suggestions?

-- SC

A:

Hi, SC. It may be hard to believe, but we have standards for scripting guy, at least for this column. We receive many questions every day. We cannot answer these questions one by one. So, how can we decide what issues to post? First, we will look for some questions that seem easier to answer. The following is a prompt:And--"AndI hope the script can do this,AndI want the script to do that,And......" -- Well, the chances of this problem at the top of the pile of problems may be relatively small. We know this is unfair, but in any case, this column is only a small part of our daily work. Therefore, we cannot spend too much time on any issue.

Another thing we are looking for will be more attractive: Will most of our audience be interested in answering this question or using it. Again, this is not always so fair, but the problems of "mainstream" technologies are generally more popular than those of esoteric technologies. Sorry.

To be honest, although we are not sure which type of script you belong to, we decided to answer this question becauseWeThis issue is very interesting. After all, why?YesRemove these spaces andVBScript is fun!ChangeVBScript is fun!What about it? The solutions we provide are as follows:

The problem is not that there are spaces in the string. If so, we can use the replace function of VBScript to remove all spaces. However, we want to keepSomeSpace. In fact, we want to have a space between every two words. We just want to removeRedundantSpace. Finally, we decided to use the replace function to replace all instances with multiple spaces with one space. Therefore, if we find seven consecutive spaces, we will replace these spaces with one.

It is very simple except for a problem. The replace function requires a string to be searched. You cannot tell it: "Well, please search instances with multiple spaces and replace them with one space ." InsteadCode, This code can replace 7 with a space:

 
Strstarter = Replace (strstarter, "")

That's great. You don't know how many spaces you need to search for. Since the string may contain 7 consecutive spaces, five consecutive spaces may exist. What should we do?

We use the following script to handle this situation. The following code will be explained later:

 Strstarter  =     " VBScript is fun!  "  

Intstarter =   Len (Strstarter)
For I = Intstarter To   2   Step   - 1
Strchars =   Space (I)
Strstarter =   Replace (Strstarter, strchars, "   " )
Next

Arrstarter =   Split (Strstarter, "   " )

For   Each Strunit In Arrstarter
Wscript. Echo strunit
Next

The last thing we do is: "Well, we need to search for a string consisting of several spaces, but we don't know how many spaces each string may contain ." This is a little troublesome until we find that, for example, this string has a total of 37 characters. That is to say, in the stringMaximumThere may be 37 consecutive spaces (assuming this string is completely composed of spaces ). Therefore, we can start by searching for 37 consecutive spaces. If we find them, we will replace them with a space. Then we search for 36 consecutive spaces, then 35, and then 34. We keep searching until the last two spaces are searched, and these spaces are replaced with one. In this case, all unnecessary spaces are removed.

Nothing new, right? And, surprisingly, it is easy to do such things. Note that in our script, we first allocate a string to the variable strstarter. Then we use the following code to determine the number of characters in strstarter:

 
Intstarter = Len (strstarter)

Let's assume that there are 37 characters. What we need to do is to keep repeating from 37 to 2. Guess what will happen? BelowYesWhat this loop does:

For I = intstarter to 2 step-1 strchars = space (I) strstarter = Replace (strstarter, strchars, "") Next

This cycle starts from the number of characters in the string, and then uses the step-1 parameter to reduce the number of characters to 2. What is step-1? By default, the for next loop increments by 1 each time. For example, the following cycle starts from 1 and then increments by 1 until the cycle reaches 10:

 
For I = 1 to 10

In our cycle, we start from the maximum number (37), and then continue to 2, each time weDecrease1. Do you understand? This is what step-1 does; it is actually a loop that runs once minus 1.

In the loop, we do two things. First, we need to createIA string consisting of spaces. Fortunately, we can use a line of code to do this, thanks to the space function:

 
Strchars = space (I)

Secondly, we need to check whether strstarter may existIA substring consisting of spaces. IfPossible, We need to replace it with a space. The following code can achieve this purpose:

 
Strstarter = Replace (strstarter, strchars ,"")

From here on, we continue to execute the loop until the last two consecutive spaces are checked. Then we exit the loop and use the split command to divide the string into an array consisting of a single word. Then -- just to prove that the above process is valid -- we echo these words.

Currently, this is not very safe. For example, if you have some extra spaces in wordsVBScriptOfFrontWhat about it? We will not go into details about this situation, but you can use the trim and rtrim functions of VBScript to remove the starting and trailing spaces. For more information about string operations, see the following link:Related sections in Microsoft Windows 2000 script writing guide.

We do not knowVBScript is fun!Environment where such a string may appear. We assume that you may encounter such a string when trying to read a fixed-length log file. If so, the above Code will be valid, but you may find thatADO(ActiveX database objects)To analyze the file.

----------------------

Space Function

Returns a string consisting of a specified number of spaces.

Space (Number)

NumberThe parameter is the number of spaces required by the user in the string.

Description

The following example usesSpaceThe function returns a string consisting of a specified number of spaces:

 Dim  Mystring
Mystring =   Space ( 10 ) ' Returns a string with 10 spaces.
Mystring =   " Hello "   &   Space ( 10 ) &   " World "   ' Insert 10 spaces between two strings.
See: http://www.chinavb.net/vbs? Url =/vbs/html/vsfctspace.htm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.