Solved

Javascript finding some value from long string

Posted on 2013-06-19
9
182 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 51

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 51

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 51

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 51

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
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…

762 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

23 Experts available now in Live!

Get 1:1 Help Now