Compare strings, find position
Basics
Compare two strings for similarity
Find position of a substrings within a string
Is a substrings within a string?
Find position of a substrings within a string after skipping part of the string
 
 



Basics
Code
<pre>
AA="My friend John is from Cuba"
BB="John"

<% AA="My friend John is from Cuba"
BB="John" %>
Is BB a substring of AA?(case sensitive)
<% if inStr (AA,BB)=0 then 
Response.write ("Yes") 
else
Response.write ("No") 
End if %>

In which position is located BB withing AA?(case sensitive)
<% =InStr (AA,BB) %> (value will be 0 in case BB is not a substring of AA)
</pre>

Resulting page
AA="john"
BB="John"
 

Is BB a substring of AA?(case sensitive)
No

In which position is located BB withing AA?(case sensitive)
11 (value will be 0 in case BB is not a substring of AA)

Compare two strings for similarity
Code
<pre>
AA="john"
BB="John"

<% AA="john"
BB="John" %>
Are AA and BB the same string?(case sensitive)
<% if StrComp (AA,BB,0)=0 then 
Response.write ("Yes") 
else
Response.write ("No") 
End if %>

Are AA and BB the same string?( no case sensitive)
<% if StrComp (AA,BB,1)=0 then 
Response.write ("Yes") 
else
Response.write ("No") 
End if %>
</pre>

Resulting page
AA="john"
BB="John"
 

Are AA and BB the same string?(case sensitive)
No

Are AA and BB the same string?(no case sensitive)
Yes

Find position of a substrings within a string

Code
<pre>
AA="12345a6789"
BB="345A"

<% AA="12345a6789"
BB="345A" %>
Which is the starting position of string BB  within AA?(case sensitive)
<% if InStr (1,AA,BB,0)=0 then 
Response.write ("BB is not a substring of AA") 
else
Response.write (InStr (1,AA,BB,0)) 
End if %>

Which is the starting position of string BB  within AA?(no case sensitive)
<% if InStr (1,AA,BB,1)=0 then 
Response.write ("BB is not a substring of AA") 
else
Response.write (InStr (1,AA,BB,1)) 
End if %>
</pre>

Resulting page
AA="12345a6789"
BB="345A"
 

Which is the starting position of string BB  within AA?(case sensitive)
BB is not a substring of AA

Which is the starting position of string BB  within AA?(no case sensitive)
3

Is a substrings within a string?

Code
<pre>
AA="the cat is nice"
BB="Cat"

<% AA="the cat is nice"
BB="Cat" %>
Is BB a substring of AA?(case sensitive)
<% if InStr (1,AA,BB,0)>0 then 
Response.write ("Yes") 
else
Response.write ("No") 
End if %>

Is BB a substring of AA?(no case sensitive)
<% if InStr (1,AA,BB,1)>0 then 
Response.write ("Yes") 
else
Response.write ("No") 
End if %>
</pre>

Resulting page
AA="the cat is nice"
BB="Cat"
 

Is BB a substring of AA?(case sensitive)
No

Is BB a substring of AA?(no case sensitive)
Yes

Find position of a substrings within a string after skipping part of the string

Code
<pre>

AA="12345A6789XXXXXX12345a678912345a678912345a678912345a678912345A6789"
BB="345A"
CC="345B"

<% AA="12345A6789XXXXXX12345a678912345a678912345a678912345a678912345A6789"
BB="345A" 
CC="345B" %>
I know BB is included in the first 9 positions of AA, but it is included in the rest of the string?(case sensitive)
<% if InStr (9,AA,BB,0)=0 then 
Response.write ("BB is not a substring of AA after position 9") 
else
Response.write ("BB is present in String AA after position 9. Its position is ") 
Response.write (InStr (9,AA,BB,0)) 
End if %>

I know BB is included in the first 9 positions of AA, but it is included in the rest of the string?(no case sensitive)
<% if InStr (9,AA,BB,1)=0 then 
Response.write ("BB is not a substring of AA after position 9") 
else
Response.write ("BB is present in String AA after position 9. Its position is ")
Response.write (InStr (9,AA,BB,1)) 
End if %>

Is CC included in string AA after position 9?(no case sensitive)
<% if InStr (9,AA,CC,1)=0 then 
Response.write ("BB is not a substring of AA after position 9") 
else
Response.write ("BB is present in String AA after position 9. Its position is ")
Response.write (InStr (9,AA,CC,1)) 
End if %>
</pre>

Resulting page
AA="12345A6789XXXXXX12345a678912345a678912345a678912345a678912345A6789"
BB="345A"
CC="345B"
 

I know BB is included in the first 9 positions of AA, but it is included in the rest of the string?(case sensitive)
BB is present in String AA after position 9. Its position is 59

I know BB is included in the first 9 positions of AA, but it is included in the rest of the string?(no case sensitive)
BB is present in String AA after position 9. Its position is 19

Is CC included in string AA after position 9?(no case sensitive)
BB is not a substring of AA after position 9

AspTutorial.info.