Position of character in a string - javascript

I have a string :
var str = "u12345a45";//position is 7 here
now i want the position of 'a'(alphabet) in that string
similarly i have few more string like this:
var str1 = "u1234567a45";//position is 9 here
var str2 = "u12345b4";//position of b is 7 here
var str3 = "u123c";//position of c is 5 here
var str4 = "u3d45";//position of d is 2 here
Now what i thought of doing is , just searching the string from last and know the occurrence of any alphabet in that strings for once.
Note:It might be any alphabet in a string like this:
var str5 = "u2233b45";//position of b is 6 here
var str6 = "u22333f45";//position of f is 7 here
any help will be appreciated .
thanks.

As simple as
str.indexOf('a') + 1
for an arbitrary non-digit character it could be
str.match(/\D/).index + 1
for the last non-digit character followed by 0..inf digit characters:
str.match(/\D\d*$/).index + 1

just use indexOf method.
var str1 = "1234567a45";
alert(str1.indexOf("a") + 1); // alerts 8

You can use JavaScript's indexOf method.
var pos1 = str1.indexOf('a'); // will equal 7
var pos2 = str2.indexOf('a'); // will equal 5
var pos3 = str3.indexOf('a'); // will equal 3
var pos4 = str4.indexOf('a'); // will equal 0

Here's a small Codesnippet that should solve your Problem.
var str1= "12345t45";
var str1Length = str1.length;
for(var a=0; a<str1Length ;a++){
if(isNaN(str1.substring(a,a+1))){
alert(str1.substring(a,a+1)+' at Position : '+(a+1));
}
}
I loope through the string and check if the actual position is a letter or a number. If it's a Letter i write him into the alert. In this Case the Alert says : 't' at Position : 6
Regards, Miriam

Related

Javascript display the last 3 numbers after the dot fixed(3) not working

I have a number which looks number this:
800.60000305176541
This number changes all the time.
So I'm doing this:
var mynumber = 800.60000305176541
var changenumber = mynumber.toFixed(3);
This is displaying 800.600 ... I need it to display the last 3 like:
800.541
How can I do this?
You can convert to string and do your manipulations.
Please note we are loosing the right most digit due to limits of javascript.
var num = 800.60000305176541;
var str = "" + num
var arr = str.split(".");
var result = arr[0]
if (arr[1]) {
result += "." + arr[1].slice(-3)
}
console.log(num)
console.log(result)
You could also try to solve it mathematically.
800.60000305176541
800.60000305176000 -
------------------
800.00000000000541
.00000000000541
10^10. X
------------------
0,541 + 800 = 800.541

How to get first 8 char and last 8 char from string using JavaScript?

How to get first 8 char and last 8 char from srting using javascript ?
I use this code for php , How can i apply this code to javascript ?
$file_name_length = mb_strlen($file_name, 'UTF-8');
$first_char_position_last_8_char_file_name= $file_name_length-8;
$first_8_char_file_name_display = mb_substr($file_name, 0, 8,"utf-8");
$last_8_char_file_name_display = mb_substr($file_name, $first_char_position_last_8_char_file_name, 8,"utf-8");
You can use String.prototype.substr():
var data = '12345678whatever87654321';
data.substr(0, 8); // 12345678
data.substr(-8); // 87654321
You can do it with substring like this:
var str = "some string with many chars";
var newstr=str.substring(0,8)+str.substr(str.length-8);
You can use String.slice or String.substring
In your case this should work:
var string = 'Hello nice nice world';
// with substring
console.log(string.substring(0, 8), string.substring(string.length - 8, string.length)); // Hello ni ce world
// with slice
console.log(string.slice(0, 8), string.slice(string.length - 8, string.length)); // Hello ni ce world
Using the same variable names you will have:
var file_name_length = file_name.length;
var first_char_position_last_8_char_file_name= file_name_length - 8;
var first_8_char_file_name_display = file_name.substr(0, 8);
var last_8_char_file_name_display = file_name.substr(first_char_position_last_8_char_file_name, 8);

how to count digits of a phonenumber(included 0) in javascript?

I am trying to write a javascript , And want to count digits of a var str,
in the code below var str is 6 digits (012345), but when i run this code it is showing answer 4. i tried to search on google but answer not found;
how to get correct answer and fix it ?
my code
var str = 012345;
var x = String(str);
var n = x.length;
document.getElementById("demo").innerHTML = "var str is[" + n + "] Digits";
Initialize you phone number as string using quotes.
var str = '0123213'
And use length property to get its length
If you were to actually have a mixed letter/number string from which you wanted to get the number of digits you could use a regex. match creates an array of all the matches in the string - in this case \d, a digit (g says to check the whole of the string, not give up the search when the first digit has been found.) You can then check the length of the returned array.
'01xx2s3eg345'.match(/\d/g).length; // 7
try replacing the code with:
var str = "012345";
var n = str.length;
document.getElementById("demo").innerHTML = "var str is[" + n + "] Digits";

Get a number after a Specific Symbol in JQuery / Javascript

I have a String Like This:
"Dark Bronze - add $120.00"
I need to pull the 120 into a float number variable.
How would I do that?
var str = "Dark Bronze - add $120.00";
var val = str.match(/\$[0-9]*\.[0-9]*/)[0];
var f = Number(val.substring(1));
// (f is a number, do whatever you want with it)
var input = 'Dark Bronze - add $120.00',
toParse = input.substring(input.indexOf('$') + 1),
dollaz = parseFloat(toParse);
alert(dollaz);
Demo →
var str="Dark Bronze - add $120.00", val;
val = parseFloat(str.slice(str.indexOf('$')));
alert('The value is ' + val);
var str = 'Dark Bronze - add $120.00';
var pos = str.indexOf('$');
if (pos < 0) {
// string doesn't contain the $ symbol
}
else {
var val = parseFloat(str.substring(pos + 1));
// do something with val
}
var str = "Dark Bronze - add $120.00";
/*
[\$£¥€] - a character class with all currencies you are looking for
( - capture
\d+ - at least one digit
\. - a literal point character
\d{2} - exactly 2 digits
) - stop capturing
*/
var rxp = /[\$£¥€](\d+\.\d{2})/;
// the second member of the array returned by `match` contains the first capture
var strVal = str.match( rxp )[1];
var floatVal = parseFloat( strVal );
console.log( floatVal ); //120

Javascript to match substring and strip everything after it

I need to match a substring X within string Y and need to match X then strip everything after it in Y.
Code
var text1 = "abcdefgh";
var text2 = "cde";
alert(text1.substring(0, text1.indexOf(text2)));
alert(text1.substring(0, text1.indexOf(text2) + text2.length));
First alert doesn't include search text, second one does.
Explanation
I'll explain the second line of the code.
text1.substring(0, text1.indexOf(text2) + text2.length))
text1.substring(startIndex, endIndex)
This piece of code takes every character from startIndex to endIndex, 0 being the first character. So In our code, we search from 0 (the start) and end on:
text1.indexOf(text2)
This returns the character position of the first instance of text2, in text 1.
text2.length
This returns the length of text 2, so if we want to include this in our returned value, we add this to the length of the returned index, giving us the returned result!
If you're looking to match just X in Y and return only X, I'd suggest using match.
var x = "treasure";
var y = "There's treasure somewhere in here.";
var results = y.match(new RegExp(x)); // -> ["treasure"]
results will either be an empty array or contain the first occurrence of x.
If you want everything in y up to and including the first occurrence of x, just modify the regular expression a little.
var results2 = y.match(new RegExp(".*" + x)); // -> ["There's treasure"]
You can use substring and indexOf:
Y.substring(0, Y.indexOf(X) + X.length))
DEMO
Of course you should test beforehand whether X is in Y.
var index = y.indexOf(x);
y = index >= 0 ? y.substring(0, y.indexOf(x) + x.length) : y;
var X = 'S';
var Y = 'TEST';
if(Y.indexOf(X) != -1){
var pos = parseInt(Y.indexOf(X)) + parseInt(X.length);
var str = Y.substring(0, pos);
Y = str;
}
document.write(Y);

Categories