By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,132 Members | 1,967 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,132 IT Pros & Developers. It's quick & easy.

strig trim algorithm

P: n/a
Hi,

Does any know a algorithm to strip heading and leading blank in a string ?
Tnansk,
Jose Luis
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Quoting jose luis fernandez diaz <jo**********************@yahoo.es>:
|
| Does any know a algorithm to strip heading and leading blank in a string ?

Since this is C++, I suppose you use std::string, so my simple example
will strip space and tab characters from:

//! \return modified string ``s'' with spaces trimmed from left
std::string& triml(std::string& s) {
int pos(0);
for ( ; s[pos]==' ' || s[pos]=='\t'; ++pos );
s.erase(0, pos);
return s;
}

//! \return modified string ``s'' with spaces trimmed from right
std::string& trimr(std::string& s) {
int pos(s.size());
for ( ; pos && s[pos-1]==' ' || s[pos]=='\t'; --pos );
s.erase(pos, s.size()-pos);
return s;
}

//! \return modified string ``s'' with spaces trimmed from edges
std::string& trim(std::string& s) {
return triml(trimr(s));
}

--
Christian Stigen Larsen -- http://csl.sublevel3.org
Jul 22 '05 #2

P: n/a


jose luis fernandez diaz wrote:

Hi,

Does any know a algorithm to strip heading and leading blank in a string ?


as long as the string has not size 0 and the first character of
the string is blank {
remove first character from string
}

as long as the string has not size 0 and the last character of the
string is blank {
remove last character from string
}
Of course variations are possible (as always: there is more then
one way to skin a cat)

n = 0;
as long as n is not greater then the length of the string and
the n-th character is blank {
n = n + 1
}

// n now contains the number of heading blank characters
remove the first n characters from the string

n = length of string
as n is greater or equal to 0 and n-th character is blank {
n = n - 1
}

// n now contains the number of trailing blank characters
remove the last n characters from the string

instead of removeing characters from a string one could also
create a substring which contains all but the characters to
remove. Which brings us to another variation:

count the number of heading blank characters -> n
count the number of trailing blank characters -> m
extract a substring which contains all but the first n and
the last m characters.
....

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #3

P: n/a
Hrm,

| std::string& trimr(std::string& s) {
| int pos(s.size());
| for ( ; pos && s[pos-1]==' ' || s[pos]=='\t'; --pos );
| // DOES NOT WORK!
| s.erase(pos, s.size()-pos);
| return s;
| }

correction:

std::string& trimr(std::string& s) {
int pos(s.size());
for ( ; pos && (s[pos-1]==' ' || s[pos-1]=='\t'); --pos );
s.erase(pos, s.size()-pos);
return s;
}

I'm sure there are better or more efficient ways of doing the trimming,
especially when dealing with other character encodings. This routine strips
whitespace, where ``whitespace'' means ``spaces and tabs''.

--
Christian Stigen Larsen -- http://csl.sublevel3.org
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.