Solved

Javascript finding some value from long string

Posted on 2013-06-19
9
211 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 55

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 55

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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 16

Author Comment

by:Gurpreet Singh Randhawa
ID: 39259672
none of the above worked, showing 'undefined' every time
0
 
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 55

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 16

Author Comment

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

2marrow
0
 
LVL 16

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 55

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

809 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