473,772 Members | 3,148 Online
Bytes | Software Development & Data Engineering Community
+ 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 12974
Quoting jose luis fernandez diaz <jo************ **********@yaho o.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::stri ng& 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::stri ng& 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::strin g& 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::stri ng& 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::stri ng& 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
12761
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' or even why there could be a problem. here is the code ////////
4
4624
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
6279
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 can i make a fast Right Trim Function in c,using Binary search kind of fast algorithm ? Offcourse...I can use the classical approach too. like : Start from the right most charecter of the string to the left of the
11
5358
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 (strIn.Substring(410, 10).Trim = "") Then 'Something processed Else 'Something processed
7
4225
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 faster? Is there a general rule/opinion to prefere members over functions? Thanks for any hint.
2
1487
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
1572
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 ( Numoccurs(char ch) ) and the second removes leading and trailing characters from a string ( Trim(string &str) ).. pleeeeeeeeeeeeeeeeeeeeeeeeeeeeeese help me
22
9753
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 thought I'd change it to the VB ..NET method "String.Trim" but that throws an object exception. Which brings the question: is it compliant to use Trim(String), or is it more within etiquette to use If Not String Is Nothing Then String.Trim?
31
2919
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 *trim(char *s) { char *begin,*end; begin = s;
0
9620
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10261
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9912
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8934
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7460
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6715
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4007
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 we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.