finding a singular W in a line of text - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

finding a singular W in a line of text

gsumnergsumner Posts: 3Member
Hi guys,

Basically I am indexing through lines of text in a text file.

I am looking to delete lines of text with a singular W in it with a space at either side. ie " W "

I would delete this line if I could find it:-

The cat sat on the W mat.

The lines of text are read in as AnsiStrings

AnsiString LineToCheck;

if (LineToCheck.AnsiPos(" W ")>0)
{
Blah Blah Blah
}

// this should find the above line but is doesn't. Any idea how I can overcome this ??

Thanks
Graham

Comments

  • Ed HallEd Hall Posts: 395Member
    : Hi guys,
    :
    : Basically I am indexing through lines of text in a text file.
    :
    : I am looking to delete lines of text with a singular W in it with a space at either side. ie " W "
    :
    : I would delete this line if I could find it:-
    :
    : The cat sat on the W mat.
    :
    : The lines of text are read in as AnsiStrings
    :
    : AnsiString LineToCheck;
    :
    : if (LineToCheck.AnsiPos(" W ")>0)
    : {
    : Blah Blah Blah
    : }
    :
    : // this should find the above line but is doesn't. Any idea how I can overcome this ??
    :
    : Thanks
    : Graham
    :
    [blue]
    Can you use strstr() to check for a substring in LineToCheck?

    Take Care,
    Ed[/blue]

  • gsumnergsumner Posts: 3Member
    : : Hi guys,
    : :
    : : Basically I am indexing through lines of text in a text file.
    : :
    : : I am looking to delete lines of text with a singular W in it with a space at either side. ie " W "
    : :
    : : I would delete this line if I could find it:-
    : :
    : : The cat sat on the W mat.
    : :
    : : The lines of text are read in as AnsiStrings
    : :
    : : AnsiString LineToCheck;
    : :
    : : if (LineToCheck.AnsiPos(" W ")>0)
    : : {
    : : Blah Blah Blah
    : : }
    : :
    : : // this should find the above line but is doesn't. Any idea how I can overcome this ??
    : :
    : : Thanks
    : : Graham
    : :
    : [blue]
    : Can you use strstr() to check for a substring in LineToCheck?
    :
    : Take Care,
    : Ed[/blue]
    :
    :
    Hi Ed, It would appear not. I think strstr() is for character strings and at this stage I cannot convert my ansistring LineToCheck into a character string to do the check. I can't understand why AnsiPos or Pos won't find the " W " correctly.

    Graham
  • Ed HallEd Hall Posts: 395Member
    : Hi Ed, It would appear not. I think strstr() is for character strings and at this stage I cannot convert my ansistring LineToCheck into a character string to do the check. I can't understand why AnsiPos or Pos won't find the " W " correctly.
    :
    : Graham
    :
    [blue]
    Is it possible the case of the W or the spaces are being read oddly? Have you tried seaching for other substrings? You've probably tried these methods, but I'm trying to help from afar. How about looking for the hex values of the spaces - x20Wx20, as referenced in the first link below. I know you've probably also scoured the web, but here are some Borland pages, if they may help:

    http://support.borland.com/thread.jspa?threadID=2046&tstart=90
    http://support.borland.com/thread.jspa?messageID=6024
    http://www.functionx.com/bcb/topics/strings.htm

    The last one has to do with C++ Builder, but maybe there's something to trigger a solution.

    Sorry I can't be of more help. Good luck with it.

    Take Care,
    Ed[/blue]

  • gsumnergsumner Posts: 3Member
    : : Hi Ed, It would appear not. I think strstr() is for character strings and at this stage I cannot convert my ansistring LineToCheck into a character string to do the check. I can't understand why AnsiPos or Pos won't find the " W " correctly.
    : :
    : : Graham
    : :
    : [blue]
    : Is it possible the case of the W or the spaces are being read oddly? Have you tried seaching for other substrings? You've probably tried these methods, but I'm trying to help from afar. How about looking for the hex values of the spaces - x20Wx20, as referenced in the first link below. I know you've probably also scoured the web, but here are some Borland pages, if they may help:
    :
    : http://support.borland.com/thread.jspa?threadID=2046&tstart=90
    : http://support.borland.com/thread.jspa?messageID=6024
    : http://www.functionx.com/bcb/topics/strings.htm
    :
    : The last one has to do with C++ Builder, but maybe there's something to trigger a solution.
    :
    : Sorry I can't be of more help. Good luck with it.
    :
    : Take Care,
    : Ed[/blue]
    :
    :
    Thankyou Ed,
    Headache over. Never worked with Hex before infact my programming is very rusty as I am updating a program I wrote years ago whilst studying c++. Thanks again for your help and patience, your a STAR!! ;-)
    Regards
    Graham
  • tsagldtsagld Posts: 621Member
    : : : Hi Ed, It would appear not. I think strstr() is for character strings and at this stage I cannot convert my ansistring LineToCheck into a character string to do the check. I can't understand why AnsiPos or Pos won't find the " W " correctly.
    : : :
    : : : Graham
    : : :
    : : [blue]
    : : Is it possible the case of the W or the spaces are being read oddly? Have you tried seaching for other substrings? You've probably tried these methods, but I'm trying to help from afar. How about looking for the hex values of the spaces - x20Wx20, as referenced in the first link below. I know you've probably also scoured the web, but here are some Borland pages, if they may help:
    : :
    : : http://support.borland.com/thread.jspa?threadID=2046&tstart=90
    : : http://support.borland.com/thread.jspa?messageID=6024
    : : http://www.functionx.com/bcb/topics/strings.htm
    : :
    : : The last one has to do with C++ Builder, but maybe there's something to trigger a solution.
    : :
    : : Sorry I can't be of more help. Good luck with it.
    : :
    : : Take Care,
    : : Ed[/blue]
    : :
    : :
    : Thankyou Ed,
    : Headache over. Never worked with Hex before infact my programming is very rusty as I am updating a program I wrote years ago whilst studying c++. Thanks again for your help and patience, your a STAR!! ;-)
    : Regards
    : Graham
    :

    Is it possible that the string is in Unicode while you are searching ASCII or something? You can detect that with a hex editor.
    Btw, ever tried using regular expressions? Little hard to bite at at first, but once you get the hang of it, they're great to search strings.


    Greets,
    Eric Goldstein
    www.gvh-maatwerk.nl


Sign In or Register to comment.