How many ways to perform a conversion of string value into integer values.Can you tell me about the ParseInt function and what is role of it. I need a project that perform a calculation on string .
How many ways to perform a conversion of string value into integer values.Can you tell me about the ParseInt function and what is role of it. I need a project that perform a calculation on string .
A string into Integer
A string is a collection of character, and if we have desire to convert it into Integer for some calculations then we can do it very easily using provided functions in java that come across into Java script .
A program that evaluate a string into integer value:
<script>
function covertint(o)
{
var strval = "023";
var result = parseInt(strval);
console.log(result);
}
</script>
Last edited by Praetor; 19-01-2010 at 11:45 AM.
There are many ways to covert a string value into integer if we look back in the previous study of java script.You can achieve it using :
- Using typeof keyword
- Using parseInt Function
Using typeof Operator:
<script>
function usetypeof()
{
var charval= "52";
var mtier = +charval;
console.log(typeof mtier);
}
</script>
Output would be :52 (in Number)
Using ParseInt Function:
<script>
function useparseint()
{
var string = "021";
var numbr = parseInt(string);
console.log(numbr);
}
</script>
Output would be : 21
Conversion of String to Integer in Java:
A program that accepts input as a string value and produce output after converting it into integer.
public class convert{
public static void main(String[] args)
{
String stringval = new String("10");//declaration of string
int mediavalue = Integer.parseInt(stringval);
System.out.println(mediavalue);
}
}
how check first 4 numbers of the mobile number.for example 9382234567
first check the first 4 numbers for 9382 .in this numbers entered in the text box after that remaining numbers are allowing otherwise it returns false message.how can coded using java script..plz reply
You will need to use an authentication in the coding so that mobile number starting from 9382 (example) will only be accepted. You can use JavaScript's Regular Expression Methods for this purpose. The standard expression method in JavaScript has two key methods for testing strings: test() and exec().
- The exec() Method
The exec() method takes one argument, a string, and checks whether that string encloses one or more matches of the pattern individual by the regular expression. If one or more matches are found, the method returns a consequence array with the starting points of the matches. If no match is found, the method returns null.- The test() Method
The test() method also takes one argument, a string, and checks whether that string contains a go with of the pattern specified by the regular expression. It returns true if it does surround a match and false if it does not. This method is very helpful in form validation scripts. The code sample below shows how it can be used for checking a social inspecting number.
Code:<html> <head> <title>Social Security Number Checker</title> <script type="text/javascript"> var AB_SSN = /^[0-9]{3}[\- ]?[0-9]{2}[\- ]?[0-9]{4}$/; function checkSsn(ssn){ if (AB_SSN.test(ssn)) { alert("VALID SSN"); } else { alert("INVALID SSN"); } } </script> </head> <body> <form onsubmit="return false;"> <input type="text" name="ssn" size="15"> <input type="button" value="Check" onclick="checkSsn(this.form.ssn.value);"> </form> </body> </html>
Bookmarks