473,466 Members | 1,514 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

strig trim algorithm

Hi,

Does any know a algorithm to strip heading and leading blank in a string ?
Tnansk,
Jose Luis
Jul 22 '05 #1
3 12956
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


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
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 thread has been closed and replies have been disabled. Please start a new discussion.

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'...
4
by: muroogan | last post by:
I read a text file into a char variable char tchar; I want to trim leading spaces (Left trim).How can I do that in C++. Any input will be appreciated.
9
by: Durgesh Sharma | last post by:
Hi All, Pleas help me .I am a starter as far as C Language is concerned . How can i Right Trim all the white spaces of a very long (2000 chars) Charecter string ( from the Right Side ) ? or how...
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...
2
by: Jan | last post by:
Hi, Is there a way to remove the white spaces between text? For example: "Section 1 is oke fine" I want it to be,
3
by: nightrosee | last post by:
hello every body how are you? I hope you are all having a nice day.. I have to write two methods, the first one counts the number of occurrences of a character ch in a string ( ...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.