CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2012
    Posts
    0

    Find substring from a string

    Hello All,

    I am stuck in some logic and want to write a program to do following tasks :
    i have three string variables to be compared to each other,a string having sub string & hierarchy string!!
    1.) name1=john name2=tom_john_tom name3=alextom_john
    thus we need to search john from name2 and name3 and if name1 exists in both name2 and name3 then ok else check for step2
    2.) name1=a.b.c.d ,name2=a.b.c.d and name3=a.b.c.d
    we need to compare each string seperated by a dot in all three variables and we need to match each string seperated by a delimeter "."
    if name1.a==name2.a==name3.a and name1.b==name2.b==name3.b ,name1.c==name2.c==name3.c and name1.d==name2.d==name3.d then its a match
    else its a mismatch

    Also,the catch is we can have name1 ,name2 and name3 in format name1=*.*.*.* and name2=*.*.*.* and name3=*.*.*.*
    where * defines it can be any value to be matched


    name1=government.india.public.property
    name2=rural.roads.maximum.x
    name3=government.india.public.sales
    name4=rural.roads.minumum.x

    If operator wants to match only the second field , then the logic should be like:

    If the Value is to be matched = (*.#.*.*)
    then "matched"
    Else
    its a mismacth

    # => for this field of name2 and name3 shall be same
    * => for this field name2 and name3 shall be ignored for comparison

    to obtain option1 we can have find function and substr however pls suggest how to approach for second option !!!!
    since i am a beginner it would be of great help thanks inadvance !!!

    Regards,
    Manas

  2. #2
    Join Date
    Mar 2004
    Location
    Central Florida
    Posts
    293

    Re: Find substring from a string

    The find() and substr() will also work for the second option. Use find the locate the periods (".") that constitue the boundries of the field you are testing. So in your example you would look for the first and second periods. Then use substr to extract the data between them. Compare the sub-string the the test value.

    If the Value is to be matched = (*.#.*.*)
    then "matched"
    Else
    its a mismacth
    Work out the logic/required stpes on a piece of paper first. then take your best shot at writing the code. After you have done that we should be able to help you debug your app.
    "Effective teaching is the essence of leadership..."

    "There is no substitute for a carefully thought-out design."

    If you have found this post to be useful, please Rate it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured