473,394 Members | 2,031 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,394 software developers and data experts.

Trim function

here i kno trim function wil remove first and last whitespaces if occured. but i don the program for that.string wil u pleas explain this program step by step?
particularly why they take the concept of takin condition >' ' in conditional statement.
program: i didnt send main,class definition simply function definition
Expand|Select|Wrap|Line Numbers
  1.           string StringUtil::Trim(string givenString)
  2. {
  3.      int count = givenString.length();
  4.      if (count == 0 || (givenString[0] > ' ' && givenString[count-1] > ' '))
  5.          return givenString;
  6.  
  7.     int preTrim = 0;
  8.     for (;; preTrim++)
  9.     {
  10.          if (preTrim == count)
  11.              return givenString;
  12.         if (givenString[preTrim] > ' ')
  13.             break;
  14.     }
  15.     int endTrim = count;
  16.      while (givenString[endTrim-1] <= ' ')
  17.         endTrim--;
  18.      return this->SubString(givenString, preTrim, endTrim-1);
  19. };
Jun 4 '07 #1
1 3541
weaknessforcats
9,208 Expert Mod 8TB
OK. Here you go:

[code=cpp]
string StringUtil::Trim(string givenString)
{
//
//This is a deprecated method. You should use givenString.size().
//None of the other STL containers have a length() but all have a size().
//
// VVVVVVVVVVVVVVVVV
int count = givenString.length();
//
//So, if the count is zero there oare no characters in the string.
//There's nothing to trim and the function returns.
//OR
//look at both ends of the string. If there's nothing to trim, the function returns.
//This is done with an expression that says
//if the first character of the string is greater than a space
//AND if the last charaacter is greater then a space, then there's
//nothing to trim and the function returns.
//
//Refer to the ASCII table. You will see that all non-space characters
//are greater than the value of a ' ' (which is 48 by the way).
//
if (count == 0 || (givenString[0] > ' ' && givenString[count-1] > ' '))
return givenString;
//
//This section skips over all leading spaces (if any).
//
// VVVVVVVVVVVVVVV
int preTrim = 0;
for (;; preTrim++)
{
if (preTrim == count)
return givenString;
if (givenString[preTrim] > ' ')
break;
}
//
//This section skips over the trailing spaces.
//
// VVVVVVVVVVVVVVVVVVVVV
int endTrim = count;
while (givenString[endTrim-1] <= ' ')
endTrim--;
//
//Finally, copy from the first non-space through the last non-space
//and put eh copy back into the original string.
//
//VVVVVVVVVVVVVVVVVVVVVVVVVV
return this->SubString(givenString, preTrim, endTrim-1);
};
]/code]
Jun 4 '07 #2

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

Similar topics

22
by: Simon | last post by:
Hi, I have written a function to trim char *, but I have been told that my way could be dangerous and that I should use memmove(...) instead. but I am not sure why my code could be 'dangerous'...
12
by: Robert Mark Bram | last post by:
Hi All, I am using the following trim function: function trim (str) { return str.replace(/^\s*/g, '').replace(/\s*$/g, ''); } The problem is that this doesn't trim instances of the...
3
by: Andy B | last post by:
I've tried using Trim or RTrim to strip trailing space characters from my data. When I check on the transformed data space characters are still there. We have an address table containing two...
11
by: Darren Anderson | last post by:
I have a function that I've tried using in an if then statement and I've found that no matter how much reworking I do with the code, the expected result is incorrect. the code: If Not...
7
by: Sascha Herpers | last post by:
Hi, what is the difference between the trim function and the trim String-member? As far as I see it, both return the trimmed string and leave the original string unaltered. Is any of the two...
22
by: Terry Olsen | last post by:
I have an app that makes decisions based on string content. I need to make sure that a string does not contain only spaces or newlines. I am using the syntax 'Trim(String)" and it works fine. I...
31
by: rkk | last post by:
Hi, I've written a small trim function to trim away the whitespaces in a given string. It works well with solaris forte cc compiler, but on mingw/cygwin gcc it isn't. Here is the code: char...
12
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM?...
5
by: Marjeta | last post by:
I'm trying to very that the user actually entered something in the form, and not just spaces. I guess the problem is in the first line of isBlank() function. I've tried the following:...
8
by: Keith Thompson | last post by:
Kevin Smith <no@spam.comwrites: You posted this to microsoft.public.dotnet.languages.csharp, where I presume it's topical. Why on Earth did you redirect followups to comp.lang.c? Anyone...
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.