Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Advanced string manipulation??

Status
Not open for further replies.

mattscotney

Programmer
Jul 31, 2002
57
AU
Hi all,

I have the following string stored in an asp variable:

myString = &quot;some regular html <script language =&quot;&quot;javascript&quot;&quot; src = &quot;&quot;aVar.js&quot;&quot;> some more html&quot;

aVar.js contained within the string is a variable and will change(eg: aVar.js will not always be known as aVar.js, it could be named welcome.vbs, or wel.js or by any other name)

<script language =&quot;&quot;javascript&quot;&quot; src = &quot;&quot; will always come before aVar.js and &quot;&quot;> will always come after.

How can I extract the aVar.js portion from the string and change it to myScript.js(string variable value) and replace it back in the string?

Thanks in advance,
Matt Scotney
 
You could have the filename stored in a variable and have your string as follows:

myFile = aVar.js
myString = &quot;some regular html <script language =&quot;&quot;javascript&quot;&quot; src = &quot;&quot;&quot; & myFile & &quot;&quot;&quot;> some more html&quot;

All you have to do is change the value of myFIle and you will change the value of your string.

Or you could use the replace method.

myString = Replace(myString, &quot;myVar.js&quot;, &quot;myScript,js&quot;)
Mighty :)
 
Mighty,
The string is read in from a web page and the aVar.js is variable so your solution will not solve the problem, as first I have to find out what is between:
<script language =&quot;&quot;javascript&quot;&quot; src = &quot;&quot;&quot; and &quot;&quot;&quot;> before I can do any replacing.

Thanks
 
Hey Matt,

you probably want to use different JS files depending on some conditions (such as browser detection, etc.). I'm not sure it'll work but I would give it a try.

document.writeln('<script language=&quot;&quot;javascript&quot;&quot;
src=&quot;&quot;'+put_here_file_you_need_or_want+'&quot;&quot;>');

But honestly, I can't understand why you use ASP code while you use on a web page (I mean client-side scripting) or am I missing something??? Good Luck! :)
 
EugenePaviev,

I dynamically generate remote pages using ASP. Eg: someone may enter in my input box and through ASP I dynamically process the page and add some extra code of my own to the page they have entered. The new page is then displayed in the browser.

I have no control over the +put_here_file_you_need_or_want+ section, but I do need to change it from a relative to exact path for my processing and extra code to work correctly.

Thanks.
 
Matt,
now I think I understand what you need... May be my code will help you.
Code:
<%
FUNCTION SetWhat()		'from what page, like attraction, disabled, etc
	Dim m_strReferrer, m_intStart, m_intFinish
	
	m_strReferrer = Request.ServerVariables(&quot;HTTP_REFERER&quot;)
	m_intFinish = InStrRev(m_strReferrer, &quot;.asp&quot;)
	m_intStart = InStrRev(m_strReferrer, &quot;/&quot;, m_intFinish)
	m_strReferrer = Mid(m_strReferrer, m_intStart+1, m_intFinish-m_intStart-1)
	
	SetWhat = m_strReferrer
End FUNCTION
%>

In this code I need to know name of the referring page. First, I put the string from which I need to extract page name into variable. I know that each page ends with &quot;.asp&quot;, so I find the position of this part (&quot;.asp&quot;) from the end of the string (InStrRev method) and keep it in variable. Then I know that before page's name there is always &quot;/&quot;, so I find the position of this symbol (again from the end of the string). Now I know starting point and ending point of page name and get it with Mid method.

Probably you need something very similar.
Good Luck! :)
 
what about

Code:
Mid(MyString,InStr(MyString,&quot;src=&quot;)+7,Instr(InStr(MyString,&quot;src=&quot;)+7,MyString,&quot;>&quot;)

I haven't tested this, but Mid takes the arguments:
Mid(string, first, last)
It returns a string.
InStr takes the arguments:
Instr(start,string,substring)
start is optional. Instr returns the numeric position of the start of the substring. Using these two functions, you can find the starting and ending positions of the file name and use mid to return it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top