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 dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to remove characters from a string

Status
Not open for further replies.

michellerobbins56

Programmer
Jan 10, 2006
89
GB
Hi

I have a string called "stringQuery" made up of characters and spaces. I have calculated the total length using
len(stringQuery)

There is a word in the string "or". I would like to remove everything to the left of stringQuery up to and including this word.

I have used the function InStr to work out where in the total string the word "or" appears. I have done this as follows:
dim characters
characters = InStr(1,strQuery, "or")

Please can anyone tell me what function or how to remove all the characters and spaces to the left of this string? Basically what I need to do is subtract 'characters' from the total string length but I'm not sure how to do this?

Thank you for any help.
 
Not to worry... I have solved my problem as follows:
dim result
result = Mid(strQuery, total_length_of_string-characters)

This works!
 
Code:
<%
myString="This and That or The Other"
myNewString=right(myString,_
     len(myString)-(instr(myString,"or")+1)_
)
response.write(trim(myNewString))
%>

-a6m1n0

Curiosity only kills cats.
 
You can also use Split.

Code:
<%
myString="This and That or The Other"

Response.Write("Stuff on the left -->" & Split(myString, "or")(0))
Response.Write("<br/>")
Response.Write("Stuff on the right ->" & Split(myString, "or")(1))
%>

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 

Michelle,

A quick word of warning...

Not sure of the content of your string, so am speculating here. If you use "or" as the split identifier (whichever method you use, split or mid or right) this could lead to false positives... for example:

stringQuery = "abc or xyz" - Will work fine
stringQuery = "corn or maize" - Will return "n or maize" for the instr methods and "c" and "n " in Georges example

I'm assuming this is a search box query on your site that you want to use to dynamically create a SQL Statement ? If so, Georges method of splitting is better as you can iterate through the array to create your "WHERE xxx or yyy" statements. However, you need to use " or " as the split value not "or" and you may want to remove any pre or post "or" values e.g. "or something or that" should be trimmed before parsing to read "something of that" then split on " or ".

Either way, I would say that unless you are 100% certain that the input will never have "or" inside a word or more than once, you should use split and " or ".

Hope that helps,

A smile is worth a thousand kind words. So smile, it's easy! :)
 
I would like to point out that the VB functions Right, Left, Mid, and InStr are perfect for parsing strings since it is the function's intended use. It is hard to get one's head wrapped around it and is why sometimes people use Split() instead--it's fewer lines of code and it is in most cases more convenient. I am not arguing one way is better than the other as I don't really know and can't say.

Also, the OP did ask specificaly for information regarding the contents left of a keyword in a string and using the these VB functions. Below is an example and two functions that I wrote just for demonstration.

Code:
<%
dim myString,myKeyword
myString="This and That or The Other"
myKeyword=" or " 'or whatever you want it to be

response.write(trim(rightOfKeyword(myString,myKeyword)))
response.write("<br>")
response.write(trim(leftOfKeyword(myString,myKeyword)))

function rightOfKeyword(rString,rKeyword)
	if len(rString)=0 then exit function
	if len(rKeyword)=0 then exit function
	if instr(rString,rKeyword)>0 then
		rightOfKeyword=right(rString,_
		len(rString)-(instr(rString,_
		rKeyword)+(len(rKeyword)-1))_
	)
	end if
end function

function leftOfKeyword(lString,lKeyword)
	if len(lString)=0 then exit function
	if len(lKeyword)=0 then exit function
	if instr(lString,lKeyword)>0 then
		leftOfKeyword=left(lString,instr(lString,lKeyword)-1)
	end if
end function
%>

-a6m1n0

Curiosity only kills cats.
 
a6m1n0,

I would like to point out that the VB functions Right, Left, Mid, and InStr are perfect for parsing strings since it is the function's intended use.

I was pointing out the potential flaw in the code that both you and George posted.. not to criticise, but to warn Michelle that if she had "or" in the text more than twice (e.g. hidden in a w[!]or[/!]d) she would not get what she expected. I gave examples to demonstrate this clearly.

The point of this was to show that the value to "split/separate" should be reviewed - which you have subsequently changed in the code in your last post. Which is good.

However, the issue now lies with the fact that " or " could occur more than once in the string (again we don't know for sure as Michelle has provided no sample data, only a derived question). If it does, then the instr method will find the position it first finds the value in, which may not be the one Michelle wants.

It is hard to get one's head wrapped around it and is why sometimes people use Split() instead--it's fewer lines of code and it is in most cases more convenient

Not really, left, right, mid are some of the most basic functions of the language, and instr isn't particularly difficult. I think most if not all programmers will know how these are generally supposed to work. Michelle showed knowledge of the functions, just wanted clarity - and in fact actually fixed it herself anyway.

I am not arguing one way is better than the other as I don't really know and can't say.

It depends on the situation and the desired output. if you want a single string output taking from the first occurance of the separator value, then the string manipulation functions will be the best option.

However if you expect to have multiple (and variable) occurances of the separator, and either want to cycle through the list of them (to dynamically build SQL statements for example) or would always like to get the last bit (or the most right as per the OPs request) then split is a better option as it returns an array you do array like things with. You can of course use revinstr to get the rightmost occurance aswell, but it will still only return 1 value unless you start creating unecessary loops or worse, recursive functions.

Also, the OP did ask specificaly for information regarding the contents left of a keyword in a string and using the these VB functions

sorry to be a little pedantic, but she asked for the information to the right implicitly by saying "Please can anyone tell me what function or how to [!]remove[/!] all the characters and spaces to the [!]left[/!] of this string?". And also asked which function to use, rather than saying it must be x, y or z.

Also, answering a question on face value is like developing application code from a users Request For Change - first rule of analysis is to find the need behind the need - what are they really needing, as opposed to what they're asking for - it is also the number 1 rule of professional selling.. why ? because people add their own understanding and interpretations to what their requirements are and don't necessarily tell the 'experts' they're asking what is really needed (usually because they don't know.. that's what they're paying you for!) - it is the 'experts' job to investigate and understand the real issue and need, and thus requirement before providing an answer. Otherwise you play "follow the scope" where the customer/TT Poster/whatever finds every answer you give either wrong, or not quite right. A part of Requirements Management, but it is useful in everyday life too.

Hope that helps to clarify without being too critical.

I doubt the OP is even reading anymore, she probably finished her app and is preparing the next release! :)

A smile is worth a thousand kind words. So smile, it's easy! :)
 
LOL damber, Thank you for spending the time to clarify your thoughts on the current topic and my reply. :D

Could you please provide an example/demonstration that illutrates a solution to the problem you propose with "This or That or Those and then some of These"?




-a6m1n0

Curiosity only kills cats.
 
This?

Code:
<%
myString="This or That or Those and then some of These"
Dim arTemp
Dim i

arTemp = Split(myString, " or ")
For i = lBound(arTemp) to UBound(arTemp)
    Response.Write(i & ": " & arTemp(i) & "<br/>")
Next
%>

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Well, it depends on what the end objective is really - that will dictate the method used and which segment of data to return.

But to demonstrate the basic dynamic sql stuff, here's a simple example (note this is written freehand, so there may be typos etc. There's also no input validation done to secure from SQL injection attacks, so as not to confuse)
Code:
dim sInputString: sInputString = "This or That or Those and then some of These"
dim aStringBits: aStringBits = split(sInputString)
dim sSQL: sSQL = "SELECT Something FROM SomeWhere "
dim sSQLWhere: sSQLWhere = "WHERE "
dim sOR: sOR = ""

for i=lbound(aStringBits) to ubound(aStringBits)
  sSQLWhere = sSQLWhere & sOR & " text_field_to_search = '" & aSrringBits(i) & "' "
  sOR = " OR "
next

if len(sSQLWhere) <= 6 then sSQLWhere = "" end if
response.writre(sSQL & sSQLWhere)

You could of course, as George shows above, just do:
Code:
for the left most segment:
aStringBits(lbound(aStringBits)) 

for the rightmost segment:
aStringBits(ubound(aStringBits)) 

all together:
dim sFullStringNoORs: sFullStringNoORs = join(aStringBits)

or without the left most segment:
aStringBits(0) = ""
dim sStringNoLeft: sStringNoLeft = join(aStringBits)

and so on...

Unfortunately I'm late for something so need to get moving.. hope that helps.

A smile is worth a thousand kind words. So smile, it's easy! :)
 
If you really want to have fun... [smile]

Code:
<%
myString="This or That or Those and then some of These"
Response.Write("FieldName = '" & Join(Split(myString, " or "), "' Or FieldName = '") & "'")
%>

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top