# Thread: Finding a substring

1. ## Finding a substring

Hey guys!

My brain has completely shut down on me and I don't know to solve this issue...

I'm trying to write a function that will find matching substrings within 2 strings. This function is going to accept 2 parameters Str1 and Str2
The goal is to find a location of a substring withing those 2 parameters and if such exists, remove that substring from Str2. This function is going to return a string that will have Str1+(what's left of Str2).

Some examples of what I am talking about are as follows
1)
STR1 = Hello all
STR2 = Hello-guys
RETURN = Hello all-guys
2)
STR1 = Hello
STR2 = All
RETURN = Hello All

The idea that I have is to split both strings into an array of characters and then try to find matching patterns that are in both strings.

Can somebody give me some pointers on how to implement this please?

Thanks for your help.

dimaj

2. ## Re: Finding a substring

Never mind this post... I found a solution...
if anybody interested, let me know and I will post it here.

dimaj

3. ## Re: Finding a substring

hi dimaj,
please post the solution here.

4. ## Re: Finding a substring

Here you go

<font class="small">Code:</font><hr /><pre>
[+] // Function Description
[ ] // this function is going to compare contents of 2 string lists
[ ] // and return list of same substrings
[ ] //
[+] // INPUT PARAMETERS
[ ] // STRING - String that we are comparing
[ ] // STRING - String that we are removing same parts from
[ ] //
[+] // RETURNS
[ ] // LIST OF STRING - substrings that are present in both strings
[+] LIST OF STRING FindSubStrings (STRING sPnS, STRING sPnN)
[ ] INT i, iCount, iPos
[ ] STRING sSubStr
[ ] LIST OF STRING lsRetVal
[ ]
[ ] // loop through the array of chars for the firts string
[+] for i = 1 to Len (sPnS)
[ ] // reset all vars
[ ] iCount = 0
[ ] sSubStr = sPnS[i]
[ ]
[ ] // see if current substring matches
[ ] iPos = StrPos (sSubStr, sPnN, FALSE)
[+] while (iPos &gt; 0)
[ ] // if it does, increment a counter
[ ] iCount += 1
[ ]
[+] if (i+iCount &lt;= Len (sPnS))
[ ] // modify the substring
[ ] sSubStr = "{sSubStr}{sPnS[i+iCount]}"
[ ] // print some info
[+] else
[ ] i += iCount - 1
[ ] break
[ ] // get new position
[ ] iPos = StrPos (sSubStr, sPnN, FALSE)
[ ] // if iPos is 0, change value of i
[+] if (iPos == 0)
[ ] i += iCount - 1
[ ]
[ ] // see if sSubStr has more than 1 character in it
[+] if (Len (sSubStr) &gt; 1)
[ ] // see if position &lt; 1
[+] if (iPos &lt; 1)
[ ] // remove the last character from the sSubStr
[ ] sSubStr = SubStr (sSubStr, 1, Len (sSubStr) - 1)
[ ] // verify that length is still more than 1 character
[+] if (Len (sSubStr) &gt; 1)
[ ] // remove that found substring from new variable
[ ] sPnN = StrTran (sPnN, sSubStr, "")
[ ] ListAppend (lsRetVal, sSubStr)
[ ]
[ ]
[ ] // return junction of both strings
[ ] return lsRetVal
[ ]
[ ]
</pre><hr />

#### Posting Permissions

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

vBulletin Optimisation provided by vB Optimise v2.6.0 Beta 4 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
User Alert System provided by Advanced User Tagging v3.0.9 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Questions / Answers Form provided by vBAnswers (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
vBNominatevBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Feedback Buttons provided by Advanced Post Thanks / Like (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Username Changing provided by Username Change (Free) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
BetaSoft Inc.
Digital Point modules: Sphinx-based search
All times are GMT -8. The time now is 06:34 PM.