'Find a specific value/characters in a string field (If statement)
I have these string statments:
- 10 - Holiday Booking
- 20 - Summer term
- 34 - Nighttime
- Autumn time
I wanted to create a IF statement which says:
If the start of the string contains 'X - ' (a number and a hyphen with spaces, although I think it will recognise the number as a string type), then keep it as it is, otherwise add a random number and hyphen to the start
- 10 - Holiday Booking
- 20 - Summer term
- 34 - Nighttime
- 55 - Autumn time
I am using Google App Scripts/Java.
Solution 1:[1]
The function can be made like this
const str1 = '10 - Holiday Booking';
const str2 = 'Autumn time';
function MyFun(str) {
const firstAscii = str.charCodeAt(0);
if( (firstAscii>=65 && firstAscii<=90 ) || (firstAscii>=65 && firstAscii<=90) ){
str = Math.floor((Math.random() * 100) + 1) + " - " + str;
}
console.log(str);
}
MyFun(str1);
MyFun(str2);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mohit maroliya |
