/    Sign up×
Community /Pin to ProfileBookmark

indexOf(,),0); problem

function display(){

p1=document.getElementById(“Prod”).heating.value = profile[0];
spacePos = p1.indexOf(“,”,0);
first = p1.substring(0,spacePos);
alert(first);

dosnt alert anything,
profile[0] is declared as 33,3232,3232,32

but if i declare p1 = “33,3232,3232,32”
it alerts me 33 which i want

why dosnt it alert from
p1=document.getElementById(“Prod”).heating.value = profile[0];
as i know that the assigning works from the profile array as it returns me the number 3232,323,32,32 when i do alert(p1);

thank you for your help

to post a comment
JavaScript

12 Comments(s)

Copy linkTweet thisAlerts:
@CharlesApr 06.2005 — Because 33,3232,3232,32 is a number and "33,3232,3232,32" is a string.

<script type="text/javascript">
<!--
alert (String (33,3232,3232,32).split (',')[0])
// -->
</script>
Copy linkTweet thisAlerts:
@patrykR1authorApr 06.2005 — how can i change it to be a string?

phaseString??

thanks
Copy linkTweet thisAlerts:
@CharlesApr 06.2005 — If I'm not mistaken, I posted an example of that above. And for kicks I tossed in a slightly more elegant way of getting that "33".
Copy linkTweet thisAlerts:
@CharlesApr 06.2005 — You can either cast the number as a String, [font=monospace]String (n)[/font] or you can call NUmber.toString() exxplicitly, [font=monospace]Number (n).toString()[/font].

The method String.split([i]s[/i]) splits the string on every [i]s[/i] and returns an Array. My example does that, returning the first element of that Array.
Copy linkTweet thisAlerts:
@patrykR1authorApr 06.2005 — Yes that is a very nice way of getting that 33

but my p=32,32,3323,22

and i want to devide it by each comma

so i can assign

p1=32

p2=32

p3=3323

p4=22

so this works for a string

name = "32,2232,323,32";

spacePos = name.indexOf(",",0);

p1 = name.substring(0,spacePos);

p2 = name.substring(spacePos+1,name.length);

something like that .. how can i divide 32,323,3232,32 by each comma and assign it each divided 32 332 etc to seperate variables>?

thank you Charles ....
Copy linkTweet thisAlerts:
@patrykR1authorApr 06.2005 — i have to use index of as the number changes so i cannont stick to the same fixed position
Copy linkTweet thisAlerts:
@CharlesApr 06.2005 — <script type="text/javascript">
<!--
p = String (33,3232,3232,32).split (',');
p1 = p[0];
p2 = p[1];
p3 = p[2];
p4 = p[3];
// -->
</script>
Copy linkTweet thisAlerts:
@patrykR1authorApr 06.2005 — Charles your the best

well that helped me out heaps, i am new to javascript

how long have u been programing??? i am a uni student from sydney
Copy linkTweet thisAlerts:
@patrykR1authorApr 06.2005 — and that worked just fine so little code all quality
Copy linkTweet thisAlerts:
@CharlesApr 06.2005 — Except that doesn't work. The commas get lost between being a number and a string. You're going to have to start the thing as a string literal.

p = '33,3232,3232,32'.split (',');
p1 = p[0];
p2 = p[1];
p3 = p[2];
p4 = p[3];
Copy linkTweet thisAlerts:
@CharlesApr 06.2005 — how long have u been programing???[/QUOTE]That's a hard question to answer. I learned FORTRAN back in 1981 or so but I'm not a programmer nor did I study programming in college. I was a Philosophy student which disciplined my mind and taught me how to learn things.
Copy linkTweet thisAlerts:
@patrykR1authorApr 06.2005 — it all good as im a processing a number of arrays where i have to seperate their number and put it into textboxes, the number is with out ' '.

thanks again
×

Success!

Help @patrykR1 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 5.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...