/    Sign up×
Community /Pin to ProfileBookmark

To find index of a string’s last character

Hi All,

I have a string “Level–1–Item–Arctic Cat–Quantity–2”, for which I want to have the output string as:

1 Artic Cat 2

The strings ‘Level–‘, ‘–Item–‘, ‘–Quantity–‘ are appended manually by me, and the ‘1’,’Artic Cat’ and ‘2’ are from variables.

Now I want to split the whole string using the strings which I have appended(‘Level–‘, ‘–Item–‘, ‘–Quantity–‘).

I am able to get the ‘1’ using

[CODE]values1 = “Level–1–Item–Arctic Cat–Quantity–2”
level = values1.substring(7,8);[/CODE]

which returned ‘1’.

Can anyone help me how to retrieve the ‘Artic Cat’ and ‘2’? I just played around the javasript string functions like substr(), indexof(), lastindexof(). But nothing worked out the way I want to.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@astupidnameFeb 10.2009 — [CODE]<script type="text/javascript">

var values1 = "Level--1--Item--Arctic Cat--Quantity--2"
var level = values1.substring(7,8);
var valuesArr = values1.split('--');
//now we have an array that looks like: Level,1,Item,Arctic Cat,Quantity,2
alert(valuesArr[0] + ": " + valuesArr[1] + "; " + valuesArr[3] + ": " + valuesArr[5]);


</script>[/CODE]
Copy linkTweet thisAlerts:
@mintedjoFeb 10.2009 — Hello.

This isn't necessarily the best solution but you could do something like this using a regular expression.

[CODE]var string = "Level--1--Item--Arctic Cat--Quantity--2"; //The string you start with
exp = /Level--(.*?)--Item--(.*?)--Quantity--(.*)/; //The regular expression
document.write([B]string.replace(exp, "$1 $2 $3")[/B]); //Use only the bits you want[/CODE]
Copy linkTweet thisAlerts:
@vaishnaviauthorFeb 10.2009 — [CODE]<script type="text/javascript">

var values1 = "Level--1--Item--Arctic Cat--Quantity--2"
var level = values1.substring(7,8);
var valuesArr = values1.split('--');
//now we have an array that looks like: Level,1,Item,Arctic Cat,Quantity,2
alert(valuesArr[0] + ": " + valuesArr[1] + "; " + valuesArr[3] + ": " + valuesArr[5]);


</script>[/CODE]
[/QUOTE]


This is what exactly I need. Thanks?
×

Success!

Help @vaishnavi spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...