Stringeling

This is a discussion on Stringeling within the C++ Programming forums, part of the General Programming Boards category; I need a solution to the following. you get string Hello World|| notice the || <--------(or any given additional garbage) ...

  1. #1
    Registered User darketernal's Avatar
    Join Date
    Sep 2001
    Posts
    41

    Stringeling

    I need a solution to the following.

    you get string

    Hello World||

    notice the || <--------(or any given additional garbage) i need to remove it, and get clean string

    Hello World

    not

    Hello World||



    Any Feedback would be greatly appreciated

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Then you have to specify what "garbage" is. If it's anything but "Hello World", then this will work:
    Code:
    char* RemoveGarbage(char* String, char* Buffer)
    {
       if(strcmp(String, "Hello World") == 0)
       {
          strcpy(Buffer, String);
       }
       else
       {
          strcpy(Buffer, "Hello World");
       }
       return Buffer;
    }
    
    int main()
    {
       char Buffer[1024];
       char* Temp = RemoveGarbage("Hello World||", Buffer);
       return 0;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User darketernal's Avatar
    Join Date
    Sep 2001
    Posts
    41
    - Dear Magos -

    Thank you very much for your reply, my apologies because the situation is akward , i will try to explain and provide you as best as possible with the information that you require.

    Your code is perfect if you know whats coming, but in my situation is variable and static(yes akward) (o/w i have no 'preknowlegde what string will be returned' , the strings are all correct exept that they return garbage like (||). So now in plain english


    i have a list of (unknown) strings. Example:

    String A||
    String B||
    String C||
    String D||
    String E||

    you get the general picture.

    When i retrieve the string i need a CLEAN string , as stated before i get

    A ||
    or Hello World ||
    or whatever || <- this is the problem , i get string with extra characters, those 'extra unwanted' characters have to be deleted, but every attempt i made nothing worked

    String - garbage (||) = clean string

    But seriously how to delete unwanted characters?

    Again any feedback is appreciated, Thank you very much in advance.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >But seriously how to delete unwanted characters?
    You search for them and remove them or copy what you want to a string you control and use that. But if by garbage you mean unusual characters after the supposed end of the string then you forgot to place a nul terminator and you're reading memory past the end of the string.

    This is the best explanation I can give without you giving us code showing the declaration of your string, how you place the values into it, and the output routine you use. Pretty little pictures only show us what you don't want, not what you're doing wrong.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You have to be specific what you mean by garbage. A computer doesn't have logic like a human, so it won't know if "Hello World*&&%#&#/@##" is a valid string or not.
    If your garbage only consists of ||, the you could search through the string until you find it and then cut it.
    Code:
    char* RemoveGarbage(char* String, char* Buffer)
    {
       int Position = 0;
       int MaxPosition = strlen(String);
    
       //Find the first garbage element
       while((Position < MaxPosition) && (String[Position] != '|'))
       {
          Position++;
       }
    
       //Copy the string into the buffer (except the garbage data)
       for(int i=0; i<Position; i++)
       {
          Buffer[i] = String[i];
       }
       Buffer[Position] = '\0';
    
       return Buffer;
    }

  6. #6
    Registered User darketernal's Avatar
    Join Date
    Sep 2001
    Posts
    41
    Junior Member Sorry,for taking so long to relply, and after saying this im probably to be crusifide , im a C++ borland 5.0 builder user *_* 'blushes'

    aight then , after my shocking confession ^_^' without futher delay

    3 items, namely

    One DirectoryListBox1
    One ListBox1
    One Edit1

    my goal = retrieve string from *Directory1->Directory* ,currently it gives all kind of wacko extensions that are useless for me ,for instance

    "c:\" = bad
    c:\|| = bad <=*check the wacko chars on the end ||

    c:\ is good, * the || signs are unfortunatly added, but not visibly , this only shows up when you retrieve the string from ListBox to edit1->text , and will give a bad string.

    When you retrieve in *Edit1* the string directly from a DirectoryListBox1 it will add unwanted "" or |||| characters to it.



    void __fastcall TForm2:irectoryListBox1MouseDown(TObject *Sender,
    TMouseButton Button, TShiftState Shift, int X, int Y)
    {
    if(Shift.Contains(ssRight))
    {
    RichEdit1->Lines->Add(DirectoryListBox1->Directory);
    ListBox1->Items->Add(DirectoryListBox1->Directory);
    }

    //----------------------------------------------
    void __fastcall TForm2::ListBox1MouseDown(TObject *Sender,
    TMouseButton Button, TShiftState Shift, int X, int Y)
    {

    Edit3->Text = ListBox1->Items->Text;
    }
    }


    Task is to GET the Directory string,

    so, in edit1 tried fetching the directory string in a listbox1

    if i click on the string in the listbox it should return the string in an Edit1->text in a G:\mp3\unknown\New Folder but i get

    G:\mp3\unknown\New Folder||

    or

    "G:\mp3\unknown\New Folder"

    but i need

    G:\mp3\unknown\New Folder

    Thanks
    Attached Images Attached Images  

  7. #7
    Registered User darketernal's Avatar
    Join Date
    Sep 2001
    Posts
    41
    I will try and work to imply everything said ASAP , I already tried to ask C++ Builder related forums to answer my question' but no one knows', since you people are so much far more advanced at the delicate art of computing and manipulation in c++ i tried to ask a question here

  8. #8
    Registered User darketernal's Avatar
    Join Date
    Sep 2001
    Posts
    41
    ack, *swallows* as i expected no replies anymore

  9. #9
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Edit3->Text = ListBox1->Items->Text;
    I've never used those classes/libraries, so I may not be of much help.
    At least it seems (from the syntax) that it should work. You set the TextEdit's text to the listBox's text.

    Also, those | characters you mentioned are in fact special characters (not letters/numbers or any other 'visible' characters) which are usually displayed as a filled square.

    Perhaps you don't get a terminating NULL character when getting the string. Try to add one manually ( '\0' ) after recieving it.

  10. #10
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by darketernal
    ack, *swallows* as i expected no replies anymore
    Take it easy
    It has only been 1.5 hours since your first post.
    Sometimes you have to wait for days for a good answer...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  11. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I many not be able to help you fully, but understanding exactly what the problem is is helpful to anyone trying to help.

    presumably DirectoryListBox->Directory is an AnsiString or TString object so it can be added to the RichEdit and List box and be viewed/handled there in addition to the Edit box. If you view the string in those components does it appear correctly and it is only when you attempt to assign the value from the ListBox to the Edit and view it in the Edit box that you get the problem? If you assign the DirectoryListBox->Directory object directly to the Edit box does it work correctly then?

  12. #12
    Registered User darketernal's Avatar
    Join Date
    Sep 2001
    Posts
    41
    I tried to imply a \0 null terminator, this is not allowed in builder, at least not in the \(slash is not allowed(=illigal character)) way.

    Yes you are correct , however i need the Listbox to 'select' the needed string.

    however returning strings from this listbox gives the additional (+garbage ||) to it.
    Last edited by darketernal; 11-07-2002 at 02:33 PM.

  13. #13
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by darketernal
    im a C++ borland 5.0 builder user *_* 'blushes'
    Nothing wrong with that.

    Those garbage characters are not "||", they are a newline character.

    I have a solution though:

    The probelm is that you're adding all of the lines to the textbox, not just the first one.
    Code:
    void __fastcall TForm2::ListBox1MouseDown(TObject *Sender,
    TMouseButton Button, TShiftState Shift, int X, int Y)
    {
    
    Edit3->Text = ListBox1->Items->Strings[0];
    }
    }
    Although you'll probably want to change the code, depending on the task to perform.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  14. #14
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    where do you get DirectoryListBox->Directory from? Are you sure it is an AnsiString/TString?

    Sang-drax may well be on to the answer however. If so, then there may well be some mechanism to identify a "selected" Item, rather than just the first Item, which is what Strings[0] is likely to provide you. This mechanism however is not standard C++, but compiler specific, so you may need to look in an online help for the appropriate syntax.

  15. #15
    Registered User darketernal's Avatar
    Join Date
    Sep 2001
    Posts
    41
    Yes it's a standard string, the listbox manipulates and adds a newline (||) garbage to it. There's no getting rid of it, if i had only a simple statement like; String minus 2 letters i would be saved.

    ideal it would be

    Hello Worlds|| - || = Hello World

    on top of that Borland is freezin up now


    i need to goto bed anyway

    To myself i wish a Happy Birthday

    22 years old on 8 nov that's in half an hour.

    And i salute everyone else, thank you very much for all your efforts.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21