IT-Consultant: James C. Fuller > Discussion

String::Remove(const TCHAR* pStr)

(1/2) > >>

Frederick J. Harris:
String::Remove() is kind of interesting.  This is one that over the years I've gone back and forth on in terms of whether to take the existing object and alter it by removing something or other from it, or maintain the existing object 'as is' and return an altered object in a second string as a return value.  The String Class as I now have it does the later.  Here is an example of that...


--- Code: ---// cl Test02.cpp Strings.cpp /O1 /Os /GR- /GS- TCLib.lib kernel32.lib user32.lib
#define UNICODE
#define _UNICODE
#include <windows.h>
#include "stdio.h"
#include "Strings.h"

int main()
{
 String s,s1;
 
 s=L"  one ,  two  ,  three,  four ,  five    ,   six";
 s.Print(true);
 s1=s.Remove(L" ");
 s1.Print(true);
 getchar();
 
 return 0;
}

#if 0

C:\Code\VStudio\VC++9\Silvah\Sil7>cl Test02.cpp Strings.cpp /O1 /Os /GR- /GS- TCLib.lib kernel32.lib user32.lib
Microsoft (R) C/C++ Optimizing Compiler Version 15.00.21022.08 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

Test02.cpp
Strings.cpp
Generating Code...
Microsoft (R) Incremental Linker Version 9.00.21022.08
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:Test02.exe
Test02.obj
Strings.obj
TCLib.lib
kernel32.lib
user32.lib

C:\Code\VStudio\VC++9\Silvah\Sil7>Test02
  one ,  two  ,  three,  four ,  five    ,   six
one,two,three,four,five,six

#endif

--- End code ---

So the key to getting that to work is to declare a 2nd String to receive the returned String with the spaces removed.

Frederick J. Harris:
Now, interestingly enough, or strangely enough, my String::Trim(), String::LTrim(), and String::RTrim() don't behave like that.  They alter the existing object.  Here is an example showing that...


--- Code: ---// cl Test03.cpp Strings.cpp /O1 /Os /GR- /GS- TCLib.lib kernel32.lib user32.lib
#define UNICODE
#define _UNICODE
#include <windows.h>
#include "stdio.h"
#include "Strings.h"

int main()
{
 String* pStrs=NULL;
 int iCnt=0;
 String s;
 
 s=L"  one ,  two  ,  three,  four ,  five    ,   six";
 s.Print(true);
 iCnt=s.ParseCount(L',');
 pStrs=new String[iCnt];
 s.Parse(pStrs,L',',iCnt);
 for(size_t i=0; i<iCnt; i++)
     pStrs[i].Print(false);
 printf("\n");
 for(size_t i=0; i<iCnt; i++)
 {
     pStrs[i].Trim();
     pStrs[i].Print(false);
 }     
 delete [] pStrs;   
 getchar();
 
 return 0;
}

#if 0


C:\Code\VStudio\VC++9\Silvah\Sil7>Test02
  one ,  two  ,  three,  four ,  five    ,   six
  one   two    three  four   five       six
onetwothreefourfivesix

#endif

--- End code ---

James C. Fuller:
Fred,
  Ok. Thanks Fred.
  Funny you should mention the trims. Those are the ones I modified so as to come in line with the ones I use and am used to in bc9.
I added a parameter so you could trim any other single char.
James


--- Code: ---changed in Strings.h:
[code]
  void LTrim(int charToTrim=32);                                                  // Removes leading white space by modifying existing String
  void RTrim(int charToTrim=32);                                                  // Removes trailing white space by modifying existing String
  void Trim(int charToTrim=32);                                                   // Removes leading or trailing white space from existing String

--- End code ---
changed in Strings.cpp

--- Code: ---void String::LTrim(int charToTrim)
{
 size_t iCt=0;

 for(size_t i=0; i<this->iLen; i++)
 {
     if(this->lpBuffer[i]==9||this->lpBuffer[i]==10||this->lpBuffer[i]==13||this->lpBuffer[i]==32||this->lpBuffer[i]==charToTrim)
        iCt++;
     else
        break;
 }
 if(iCt)
 {
    for(size_t i=iCt; i<=this->iLen; i++)
        this->lpBuffer[i-iCt]=this->lpBuffer[i];
 }
 this->iLen=this->iLen-iCt;
 this->blnSucceeded=TRUE;
}

void String::RTrim(int charToTrim)
{
 int iCt=0;

 for(int i=this->iLen-1; i>0; i--)
 {
     if(this->lpBuffer[i]==9||this->lpBuffer[i]==10||this->lpBuffer[i]==13||this->lpBuffer[i]==32||this->lpBuffer[i]==charToTrim )
        iCt++;
     else
        break;
 }
 this->lpBuffer[this->iLen-iCt]=0;
 this->iLen=this->iLen-iCt;
 this->blnSucceeded=TRUE;
}

void String::Trim(int charToTrim)
{
 this->LTrim(charToTrim);
 this->RTrim(charToTrim);
 this->blnSucceeded=TRUE;
}


--- End code ---

Frederick J. Harris:
Wow!  That's cool.

Fred

James C. Fuller:
Fred,
  This seems to work. Do you see any problems reusing s1?
James


--- Code: ---typedef String fstring;
int _tmain ()
{
    fstring  s1(_T("Zero,  One, Two  , Three ,  Four, Five   , Six, Seven, Eight , Nine, Ten,"));
    s1.Print( true);
    s1 = s1.Remove(_T(" "));
    s1.Print( true);
    // Remove Trailing comma
    s1.RTrim(44);
    s1.Print( true);
}

--- End code ---

output:

Zero,  One, Two  , Three ,  Four, Five   , Six, Seven, Eight , Nine, Ten,
Zero,One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten,
Zero,One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten

Navigation

[0] Message Index

[#] Next page

Go to full version