Click to See Complete Forum and Search --> : How to get partial text item?
BrainDonor
06-10-2003, 11:17 PM
I'm baaaack...:D
Hi everyone,
Lets say I have a 10 character variable called 'mytext' that I only want to view the last 8 characters...
If the SQL world the syntax would be mytext[2,10].
How would I do this in Java?
Any thoughts?
Thanks for your help!
Tom
Khalid Ali
06-10-2003, 11:36 PM
Java or javascript both have pretty much the same syntax.
string.substring(start,end);
var string = "JavaScript is Different then Java"
alert(string.substring(2,10))
returns exactly as you had in sql
mytext[2,10].
BrainDonor
06-10-2003, 11:42 PM
Thanks much Khalid! Would I use the same logic then to test one character?
Tom
BrainDonor
06-10-2003, 11:57 PM
Should probably mention what I am trying to do is test the second character in a string to see if it's a comma...if so, reset it to nothing.
Thanks again!
Tom
Khalid Ali
06-11-2003, 12:01 AM
for that you might wanna do this
if(string.charAt(1)==","){
//second char is comma
}else{
//second char is not comma
}
BrainDonor
06-11-2003, 12:18 AM
Not sure if this was the right thing to do or not, but I took two of your suggestions and blended them together. Here's how it ended up...seems to work quite well...
if (r < 0) {
var v = q;
var b = v.length;
if (v.substring(2,0) == '-,'){
var m = '-' + v.substring(2,b);
q = m;
}else{
q = q;
}
}
Thanks again for your help! I love this forum! :D
Tom
Charles
06-11-2003, 04:55 AM
Originally posted by TomWeinstock
Should probably mention what I am trying to do is test the second character in a string to see if it's a comma...if so, reset it to nothing. <script type="text/javascript">
<!--
alert ('f,oo'.replace(/^(.),/, '$1'));
// -->
</script>
And see http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/string.html#1194258.
BrainDonor
06-11-2003, 06:47 AM
Wow, that's a lot easier than what I came up with. Thanks Charles. Can you explain what each part of the replace is doing? I want to make sure I understand it. Thanks again.
Tom
Charles
06-11-2003, 02:09 PM
See http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/string.html#1194258.
BrainDonor
06-11-2003, 05:56 PM
Thanks Charles. Lots of cool stuff there! :)
Tom