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
Printable View
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
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;
}
- 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.
>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
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;
}
:o Junior Member:o :( 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::DirectoryListBox1MouseDown(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
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 :rolleyes:
ack, *swallows* as i expected no replies anymore :(
I've never used those classes/libraries, so I may not be of much help.Quote:
Edit3->Text = ListBox1->Items->Text;
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.
Take it easy ;)Quote:
Originally posted by darketernal
ack, *swallows* as i expected no replies anymore :(
It has only been 1.5 hours since your first post.
Sometimes you have to wait for days for a good answer...
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?
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.
Nothing wrong with that.Quote:
Originally posted by darketernal
im a C++ borland 5.0 builder user *_* 'blushes'
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.
Although you'll probably want to change the code, depending on the task to perform.Code:void __fastcall TForm2::ListBox1MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
Edit3->Text = ListBox1->Items->Strings[0];
}
}
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.
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 :eek:
i need to goto bed anyway :rolleyes:
To myself i wish a Happy Birthday :D
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.
Not exactly garbage. You're converting all the items in the listbox to a single string. Newline is the separator.Quote:
Originally posted by darketernal
listbox manipulates and adds a newline (||) garbage to it.
:eek:Undo separator?:confused:
He darketernal,
Van harte gefeliciteerd met je 22ste verjaardag. ;)
:o:D Dank u Zeer :D:o
( i solved it like this ) *take in context it's C++ Builder*
Since i had no idea how to (mouse click is the listbox value) i had to figure out how i could assign (as doggiesnot stated) ListBox1->Items->Strings[i] , Getting [i] was very difficult,but it was the right choise.
My code became the Following
Added 1 CSpinEdit1.
This gave a clean string without the garbage.Code:SpinEdit1->Value = ListBox1->ItemIndex;
Edit4->Text = ListBox1->Items->Strings[CSpinEdit1->Value];
So instead of trying to delete the separators and since i invested much time in getting code trying to erase newline's , i ditched that idea did like above, and came to a solution.
Thank you so much. And also for everyone elses views in this.