[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

splitting string and checking that segments exist

Posted on 2011-09-14
20
Medium Priority
?
243 Views
Last Modified: 2012-05-12
If i have a string like "purchase_click_AmazonUK_XBOX360" I want to split it and grab the last 2 values (AmazonUK and XBOX360 in this case) but I also need to check if something exists in these positions and if not give a placeholder value of "NONE"
0
Comment
Question by:894359
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 9
20 Comments
 
LVL 41

Expert Comment

by:HonorGod
ID: 36539266
something like this?
<html>
<body>

<script type="text/javascript">

var s = "purchase_click"
var n = s.split( '_' )
if ( n.length < 3 ) {
  n.push( 'None' )
}
if ( n.length < 4 ) {
  n.push( 'None' )
}
document.write( n )

</script>

</body>
</html>

Open in new window

0
 

Author Comment

by:894359
ID: 36539346
I actually only want the last 2 values combined to make a new string with a ":"  so for example:

"purchase_click_AmazonUK_XBOX360" --> "AmazonUK:XBOX360"

"purchase_click_AmazonUK" --> "AmazonUK:NONE"

"purchase_click" --> "NONE:NONE"
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 36539568
use : str.split(/purchase_click|_/).slice(2).concat(["NONE","NONE"]).slice(0,2).join(":")

test page : http://jsfiddle.net/QyVQp/
str = "purchase_click_AmazonUK_XBOX360"; // --> "AmazonUK:XBOX360"
str = str.split(/purchase_click|_/).slice(2).concat(["NONE","NONE"]).slice(0,2).join(":");
alert(str);

str = "purchase_click_AmazonUK"; // --> "AmazonUK:NONE"
str = str.split(/purchase_click|_/).slice(2).concat(["NONE","NONE"]).slice(0,2).join(":");
alert(str);

str = "purchase_click"; // --> "NONE:NONE"
str = str.split(/purchase_click|_/).slice(2).concat(["NONE","NONE"]).slice(0,2).join(":");
alert(str);

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:894359
ID: 36539807
Hi Leakim,
I can't assume that the first part of the string will always be "purchase_click" it could be other values. Also I wasn't seeing the results I expected from the code sample you provided?  Every example came out "NONE:NONE"

Thanks,
MJ
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36539851
>Every example came out "NONE:NONE"

with the test page? incredible!
0
 

Author Comment

by:894359
ID: 36539968
You lost me?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36540006
click on the link and confirm you get each time NONE:NONE,
thanks
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36540111
0
 

Author Comment

by:894359
ID: 36540122
No... I got the expected values. I still can't use the string "purchase_click" as it may change but there will always be a pattern of "word underscore word"

Thanks!
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36540133
try with firefox, the error you get come from jsfiddle not from the script itself
once you confirm it work (because it use standard javascript function) we can do additional works
0
 

Author Comment

by:894359
ID: 36540140
One weird thing is when I copy your code and run it locally I get all "NONE" as mentioned but the same code returns different results on your example page? I have no idea why?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36540146
try with firefox, the error you get come from jsfiddle not from the script itself
0
 

Author Comment

by:894359
ID: 36540181
I get the proper results in FF but not IE.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36540190
Wouldn't be "word underscore word underscore" ?

one of these word can be empty?

for example :

underscore word underscore

or again :

underscore underscore

0
 

Author Comment

by:894359
ID: 36540194
Just changed to this and it worked:

str = "purchase_click_AmazonUK_XBOX360"; // --> "AmazonUK:XBOX360"
str = str.split("_").slice(2).concat(["NONE","NONE"]).slice(0,2).join(":");
alert(str);

str = "purchase_click_AmazonUK"; // --> "AmazonUK:NONE"
str = str.split("_").slice(2).concat(["NONE","NONE"]).slice(0,2).join(":");
alert(str);

str = "purchase_click"; // --> "NONE:NONE"
str = str.split("_").slice(2).concat(["NONE","NONE"]).slice(0,2).join(":");
alert(str);
0
 

Author Comment

by:894359
ID: 36540196
>Wouldn't be "word underscore word underscore" ?

No it will always end with a word and never an underscore.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36540208
>Just changed to this and it worked:

so happy end?!
0
 

Author Comment

by:894359
ID: 36540230
Yes for now! :0)
0
 

Author Closing Comment

by:894359
ID: 36540233
Please see corrections for IE in thread.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36540234
^^;
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question