 |
| Javascript How-To Ask your "How do I do this with Javascript?" questions here. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

November 18th, 2004, 04:50 PM
|
|
Authorized User
|
|
Join Date: Nov 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
String Manipulation, dropping the first character
I am trying to develop a function that will strip the first letter of a text string if the first letter is "P", and keep everything else.
|
|

November 18th, 2004, 05:10 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Cool. Let us know when you're done and please post the code!!
But seriously, do you have a specific question, or do you want us to write the code for you??
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
|

November 18th, 2004, 05:26 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 344
Thanks: 0
Thanked 1 Time in 1 Post
|
|
try something like :
function stripIfP(word)
{
if (word[0].toUpperCase()=='P')
{
word=word.substring(1);
}
return word;
}
|
|

November 24th, 2004, 12:24 PM
|
|
Authorized User
|
|
Join Date: Nov 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks for that. I was looking for a suggestion, a place to start, or an example. But since you seem to be too 'advanced' to understand us lowely beginners, maybe you should stick to code. Talking to real people has become to laborsome for you.
Quote:
quote:Originally posted by Imar
Cool. Let us know when you're done and please post the code!!
But seriously, do you have a specific question, or do you want us to write the code for you??
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
|
|

November 24th, 2004, 02:01 PM
|
|
Authorized User
|
|
Join Date: Nov 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
A genuine 'Thanks' to Greg. Here's what I used in the end:
function checkPN()
{
var teststring = EQRepair1.EQPart1.value;
if ((teststring.substring(0,1)) == "P")
{
EQRepair1.EQPart1.value = teststring.substring(1);
}
}
|
|

November 24th, 2004, 02:24 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
IMO, it's dangerous to make assumptions about things you don't know nothing about.
If you took some time to read some of my posts, you'll find that I *do* understand newbies, and try to help them wherever I can.
However, your initial question is not very helpful to begin with. If you ask a question, it's often useful to supply some background. Describe what you already tried, what worked and didn't work, what resources you consulted etc etc.
The way you posed your question sounded like you wanted someone else to do your work. Apparently *you* were the one thinking in code only, as you didn't provide a background story, just needed the code.
And maybe my humour may not always be appreciated, but I was trying to make a joke. That's why I also asked for a more specific question.
I don't want to start a flame war here; I just don't like it when people make assumptions about my real life.....
Imar
|
|

November 26th, 2004, 08:05 AM
|
|
Authorized User
|
|
Join Date: Nov 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
My apologies. No, I didn't appreciate your brand of humor. I'm sorry if my question was vague, but I hadn't tried anything because I didn't know where to start. I missed the 'substring' keyword when I was searching for a solution on my own, otherwise I would have figured it out.
Thanks for the feedback.
Quote:
quote:Originally posted by Imar
IMO, it's dangerous to make assumptions about things you don't know nothing about.
If you took some time to read some of my posts, you'll find that I *do* understand newbies, and try to help them wherever I can.
However, your initial question is not very helpful to begin with. If you ask a question, it's often useful to supply some background. Describe what you already tried, what worked and didn't work, what resources you consulted etc etc.
The way you posed your question sounded like you wanted someone else to do your work. Apparently *you* were the one thinking in code only, as you didn't provide a background story, just needed the code.
And maybe my humour may not always be appreciated, but I was trying to make a joke. That's why I also asked for a more specific question.
I don't want to start a flame war here; I just don't like it when people make assumptions about my real life.....
Imar
|
|
|

November 29th, 2004, 01:54 PM
|
|
Authorized User
|
|
Join Date: Nov 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Well, as with most projects, a wrench has been thrown in. Now, instead of stripping the 'P' in all cases, we need to keep the 'P' if the second character is also an alphanumeric character. Here's what I have come up with (and it works!):
function checkPN()
{
var testExp = /\d/;
var teststring = EQRepair1.EQPart1.value;
if (((teststring.substring(0,1)) == "P") && (testExp.test(teststring.substring(1,2))))
{
EQRepair1.EQPart1.value = teststring.substring(1);
}
}
For Example, if the number in question is 'P12G0080', then the function will return '12G0080'. If the number in question is 'PB00755336' then the function will not change it.
|
|

November 30th, 2004, 03:20 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi Oliver,
I am not too good with regular expressions, so I might be talking complete crap here, but I think you can also use this code:
Code:
function checkPN()
{
var testExp = new RegExp("^P\\d",'i');
var teststring = 'P110755336';
if (testExp.test(teststring))
{
alert(teststring.substring(1));
}
}
The pattern passed as the first argument to the RegExp constructor says something like this:
^ - Start of the string. This way, your value has to start with the pattern. This blocks a number like 123P3245
P - or p indicates the literal value of a p (or a P) (since the test method is now case-insensitive, see later)
\\d - Escaped version of \d which is the same number check you used before. You need to escape the \ with an additional \ to be able to pass it as a string to the RegExp constructor. I think it's good practice not to pass patterns as string, because this escaping makes your code harder to read. However, I couldn't make it work without the quotes, so I escaped them anyway..... ;)
The "i" passed to the constructor makes the Regular Expression case insensitive, so it doesn't matter if the value starts with a P or p. You can leave it out if your requirements demand a fixed case.
HtH,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Red Right Hand by Nick Cave & the Bad Seeds (Track 5 from the album: Let Love In) What's This?
|
|

December 1st, 2004, 04:56 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Or even...
Code:
function checkPN(){
var fld = EQRepair1.EQPart1;
fld.value = fld.value.replace(/^P(\d.+)$/i, "$1");
}
Cheers,
Chris
|
|
 |