Solved

Javascript finding some value from long string

Posted on 2013-06-19
9
197 Views
Last Modified: 2013-06-21
Hi i have a string like this :

22208 : INCISE SPINE 3 COLUMN ADL SEGücertified~12/12/2012

The above string has following parts

Part 1: 22208 : INCISE SPINE 3 COLUMN ADL SEG [can be of any Length]

Part 2: ü [this is used as separator]

Part 3: certified [can be of value certified/noncertified/pending]

Part 4: ~ [this is again used as separator]

Part 5: 12/12/2012 [Date can be entered as mm/dd or mm/dd/yyyy or yyyy]

so i want do the part 3 and part 4 in separate alerts but i am caught in a mess here, my JS is not strong enough, here is my bit of try:

var index=myObject[row].indexOf("ü");
				var index2=myObject[row].indexOf("~");
				var statusCPT = myObject[row].substr(index+1)
				var statusCPT = statusCPT
				var statusDate = myObject[row].substr(index2+1)
				alert(statusCPT);
				alert(statusDate);

Open in new window

0
Comment
  • 4
  • 3
  • 2
9 Comments
 
LVL 53

Assisted Solution

by:Julian Hansen
Julian Hansen earned 250 total points
ID: 39259265
Do you man part 3 and 5 (4 appears to be a separator)

Why not find the first separator - change it to the second separator and then use the split function to get what you need?

Off the top of my head - have not had time to test

var indx = myObject[row].indexOf("ü");
myObject[row][indx] = '~';
var strs = myObject[row].split("~");
alert(strs[1]);
alert(strs[2]);
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 250 total points
ID: 39259289
use split with regex:
var txt = myObject[row];
var tokens = txt.split(/[ü~]+/);
var part1 = tokens[0];
var status = tokens[1];
alert(status);
var date = tokens[2];
alert(date);

Open in new window

0
 
LVL 53

Expert Comment

by:Julian Hansen
ID: 39259318
Tested version using replace
var strs = myObject[row].replace("ü","~").split("~");
alert(strs[1]);
alert(strs[2]);

Open in new window

0
 
LVL 15

Author Comment

by:Gurpreet Singh Randhawa
ID: 39259672
none of the above worked, showing 'undefined' every time
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 42

Expert Comment

by:sedgwick
ID: 39259981
I used the example u posted in the question.
Can u post the exact string u tested?
0
 
LVL 53

Expert Comment

by:Julian Hansen
ID: 39260302
You have not posted how you get your data - where it comes from what it looks like. No other code other than the snippet you tried to solve the problem.

Based on your original post to working suggestions have been made.

You say they don't work - but you don't say

a) How you tested them
b) What data you tested them with.

It would be better for all concerned if you gave us as much info as possible in each of your posts so that we have a complete picture of

a) What you are wanting to do
b) What you have tried to do.
0
 
LVL 15

Author Comment

by:Gurpreet Singh Randhawa
ID: 39260307
will do, i am ooo now..

2marrow
0
 
LVL 15

Author Comment

by:Gurpreet Singh Randhawa
ID: 39265163
Tried the way i am doing it (I changed the ~ to S)

Here is my build in script:

destinationObject[destinationObject.length]=document.frmDiagEdit.DiagnosisCPT.value+'ü'+newstatus+'S'+newdateStatus;

the Last symbol can be available and it cannot be available . I am referring to this:

+'S'+newdateStatus;

on the all the string is build like this:

22208 : INCISE SPINE 3 COLUMN ADL SEGücertifiedS12/12/2012

it should give me 3 different rows in alerts separating all three and if the date is not defined at all, it show 2 only

that's all is in the script
0
 
LVL 53

Expert Comment

by:Julian Hansen
ID: 39265213
If you are building the string yourself why not put in a standard delimiter - why put in the 'ü' and 'S'?

You can then use the standard split function to generate an array of strings which is then dead simple?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how can display objects ? 3 25
Change javascript css 1 21
How can i make my code return a zero when there are no numbers in the array? 6 27
jQuery Validate 4 22
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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'…
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…

863 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now