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

String manipulation

Status
Not open for further replies.

craigward

Programmer
Nov 13, 2007
230
GB
Hi,

I have a string that will vary in length but there will always be a consistent pattern.

Var String = “value a; value b; value c;”

This string could go on with additional value value e; value f; etc etc.

What I need to do is to take that variable “string” and create a new variable and alter the values in the string. Again it will always be consistent value I will be creating from it.

For example the above var string will need to look like newstring below, it is building a SQL query actually:

Var newstring = “ car like ‘%value a % OR car like ‘%value b % OR car like ‘%value c %’

And if there were more values I would need it to keep building the string dynamically.

What is the best method for me to do this and does anyone have some sample code I can work with?

Thanks for looking.
 
Hi

Are you sure you need JavaScript ?
[ul]
[li]You used a "Var" keyword, which is invalid in JavaScript.[/li]
[li]As most SQL is executed server-side, sounds pointless to generate SQL statements in JavaScript.[/li]
[/ul]
Until you confirm you really need JavaScript, here is a simple JavaScript 1.6 solution ( supported by Explorer only since version 9 ) :
JavaScript:
newstring[teal]=[/teal]String
  [teal].[/teal][COLOR=darkgoldenrod]split[/color][teal]([/teal][fuchsia]/ *; */[/fuchsia][teal])[/teal]
  [teal].[/teal][COLOR=darkgoldenrod]filter[/color][teal]([/teal][b]function[/b][teal]([/teal]p[teal])[/teal][teal]{[/teal][b]return[/b] p[teal].[/teal][COLOR=darkgoldenrod]trim[/color][teal]()[/teal][teal]}[/teal][teal])[/teal]
  [teal].[/teal][COLOR=darkgoldenrod]map[/color][teal]([/teal][b]function[/b][teal]([/teal]p[teal])[/teal][teal]{[/teal][b]return[/b] [green][i]"car like '%"[/i][/green][teal]+[/teal]p[teal].[/teal][COLOR=darkgoldenrod]replace[/color][teal]([/teal][fuchsia]/['\\]/g[/fuchsia][teal],[/teal][green][i]'[/i][/green][lime][i]\\[/i][/lime][green][i]$&'[/i][/green][teal])+[/teal][green][i]" %'"[/i][/green][teal]}[/teal][teal])[/teal]
  [teal].[/teal][COLOR=darkgoldenrod]join[/color][teal]([/teal][green][i]' or '[/i][/green][teal])[/teal]

Feherke.
feherke.github.io
 
Sorry, yes it is JavaScript. I put Var by mistake because i am using vb-script for something else. I have to have this string formatted for my SQL query because i pass that to another page and at that point it has to be correctly formatted for SQL.

I will try your script and see how i get on. Many thanks.
 
well 'var' in and of itself isn't incorrect, it's just the upper casing on the 'V' that's the issue.




"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
Hi,

I have tried the following code and must have something wrong as the result is not correct.

I am only building part of an SQL query here and when passed to the next page that is added to the full query.

Thanks for looking

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd">[/URL]
<html>
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript">
function test(){

string = "value a; value b; value c;"

newstring=String
  .split(/ *; */)
  .filter(function(p){return p.trim()})
  .map(function(p){return "car like '%"+p.replace(/['\\]/g,'\\$&')+" %'"})
  .join(' or ') 
  
  alert(newstring)
  
  
}

</script>
</head>

<body onload="test()">

</table>
</body>
</html>
 
Perfect, thank you so much that is a great function to know.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top