This question already has answers here:
How to evaluate a math expression given in string form?
(26 answers)
Closed 6 years ago.
An input of string is passed as an argument to a comptute function.The string can contain up to three
values of numbers and some operators like * - / +? For example, "25.7-14*34" is valid
This is the format:
public double compute(String input){
...
}
I think I would need a condition to check if a character is an operator but I don't know how to check for it in Java. Can someone please help me out?
You can use Character class in case you are planning to do it yourself.
Character.isLetterOrDigit(), Character.getType(c) == Character.MATH_SYMBOL to check whether the character passed c is a math symbol or not.
There are a lot of other static methods present in Character class which you can make use of.
Related
This question already has answers here:
What is the easiest/best/most correct way to iterate through the characters of a string in Java?
(16 answers)
Get string character by index
(13 answers)
Closed last month.
I want to be able to more easily spell out words in an expanded format. I want it like the Python code:
import time
word = input()
for i in range(len(word)):
print(word[i])
time.sleep(1)
This question already has answers here:
Using explicitly numbered repetition instead of question mark, star and plus
(4 answers)
Closed 2 years ago.
I have this regular expression that I wrote, which extracts text in between tags like "#<string to extract>":
"#<(.+?)>"
I need to make sure that the length of the string I'm extracting is 6 and my current solution is checking the length of the string that I extracted with an if statement. I would like to replace this with a regex instead. How could I modify "#<(.+?)>" to make sure it is 6 characters when extracted?
You can use curly braces to specify the length of the match.
#<(.{6})>
This question already has answers here:
Why does Apache Commons consider '१२३' numeric?
(5 answers)
Closed 6 years ago.
I was reading the commons.apache.org isNumeric method definition and it states:
StringUtils.isNumeric("???") = true;
I am not sure why "???" is considered to be numeric. My guesses are:
A "?" is considered a unicode digit
It is some kind of regex pattern
I was able to find the answer to this question by looking at the StringUtils source code for the isNumeric method.
In the source code that line appears as:
StringUtils.isNumeric("\u0967\u0968\u0969") = true
Where u0967, u0968, u0969 are Devangari Digits one, two, and three respectively.
This may be a browser issue causing the characters to not be rendered correctly in the API.
Looking at the code, the example is
StringUtils.isNumeric("\u0967\u0968\u0969") = true
\u0967 is १, which is "Devanagari Digit One"
\u0967 is २, which is "Devanagari Digit Two"
So they are digits!
This question already has answers here:
How to check a string starts with a substring or not in java?
(2 answers)
Closed 7 years ago.
I have figured it out how to check if a string is present in another string. But I need to know how can we check if a string is present at the very start of another string.
for example:
String str1="first not";
String str2="not last";
I need a condition to check if "not" is present at the start of the string which should be satisfying only for the case of str2 rather than both.
You have startsWith method in String class.
yourString.startsWith("not")
This question already has answers here:
How to extract numbers from a string and get an array of ints?
(13 answers)
Closed 8 years ago.
As i'm really bad in REGEX, I'm looking for some help here. a string can be of the following two formats:
1. id://3
2. id://3/1
How can i check if one of the two formats is available (because there's e.g. id://next – id + strings – as well) and how can i extract in the first case the "3" and the second case the "3" and "1" in separate variables? the numbers can be anything (3,1 is just an example).
thanks in advance!
You can use this regex:
\bid:\/.*?\/(\d+)\b(?!\/)
RegEx Demo