/    Sign up×
Community /Pin to ProfileBookmark

funky substring

extracting data from a text box using substring

example string = ‘f /’

test = substring(2,1) is giving me ‘ /’

…why is it giving me 2 characters when it specifies only 1 character??

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@MichaelMDec 11.2003 — Can you post the script as you have it? Based on what you've shown us, I can't see why you'd be getting bad results from the substring method.
Copy linkTweet thisAlerts:
@David_HarrisonDec 11.2003 — You are using the substring method incorrectly. It should be used like this:

var str='f /';

test=str.substring(1,2);

This will assign the variable test a value of " ". For substring you specify two numbers, the first is where it should start taking characters from (remembering that the numbering of char's starts from 0), and the second is the number of the character after where you want it to stop. So if (for some reason) you wanted all the string you would do this:

test=str.substring(0,str.length);

or, in this case:

test=str.substring(0,3);
Copy linkTweet thisAlerts:
@kbrown2974authorDec 11.2003 — thanks..i'm used to the substring(1,2) being start pos 1, 2 char..not start 1, end 2
Copy linkTweet thisAlerts:
@David_HarrisonDec 11.2003 — Happy to help. ?

I think that is what it's like in VBScript but I can't be sure.
Copy linkTweet thisAlerts:
@oleragDec 11.2003 — Yep - the difference between "substr" and "substring"

functions. You can see in this example that "substr"

works similar to Oracle's pl/sql language while the

"substring" is like Java's language.

If you cut/paste the example below and input... oh "abcdef"

as the string you'll see the different strings. Javascript also

attempts to fix things in "substring" if your second argument

is < the first argument by, I believe switching the values,

which is probably what your example is doing (changing the

(2,1) to (1,2) thereby getting your "space and slash").

In your example if your replaced "substring" with "substr"

you'd get what your looking for or you can add one to the

first argument's value and place this amount in the second

argument using "substring) to only see the slash.

[code=php]
<html>
<head>
<script type="text/javascript">
function showText() {
var objValue = document.forms[0].myText.value;
alert("JavaScript Substringn " +
"Substring: " + objValue.substring(3,4) + "n" +
"Substr: " + objValue.substr(3,4));
}
</script>
</head>
<body>
<center>
<b>Title</b>
<form>
<input type="Textfield" name="myText" length="25">
<br>
<input type="Button" name="showTextBtn" value="Show Text" onClick="showText()">
</form>
<hr>
</center>
</body>
</html>
[/code]
×

Success!

Help @kbrown2974 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.16,
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,
)...