strig trim algorithm

  • Thread starter jose luis fernandez diaz
  • Start date
J

jose luis fernandez diaz

Hi,

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


Tnansk,
Jose Luis
 
C

Christian Stigen Larsen

Quoting jose luis fernandez diaz <[email protected]>:
|
| 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));
}
 
K

Karl Heinz Buchegger

jose said:
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.


....
 
C

Christian Stigen Larsen

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''.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,147
Messages
2,570,833
Members
47,380
Latest member
AlinaBlevi

Latest Threads

Top