This question already has answers here:
Java String.indexOf and empty Strings
(7 answers)
Closed 4 years ago.
I Just tried to find out what the compiler does if I try to find the index of a null charachter in a String.I remember somehwere that a null charachter is always appended to the end of a string, so i expected the code below to give me 5.
When I type out this:
String s = "Hello";
System.out.println(s.indexOf(""));
It gives an output of 0
Help please!
First, if you were looking for a null character you would probably want to do (char)0 because "" is an empty string (no character).
Second, if Java uses a null character (they don't have to IIRC) then they hide it.
EDIT: Struck my third point because I'm not sure about it anymore.
SECOND EDIT: I looked it up, Java strings are not null terminated. They rely on the length field.
Related
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 an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
scanner.useDelimiter("\\A").hasNext() ? scanner.next() : "";
What is the significance of "\\A" here? As far as I know this will match literally \A
Also I have seen people using "\\Z" at the same place.
In regex
\A indicates start of String
\z indicates end of String
For more details, refer below the link.
https://www.rexegg.com/regex-quickstart.html
This question already has answers here:
Difference between null and empty ("") Java String
(22 answers)
Closed 6 years ago.
In my computer science class we were discussing null and values when we came across a predicament. We could not figure out the value of simply 2 quotes with no space as "". Just wondering if anyone would know what the exact value of "". Thanks
It's the empty String. JLS-3.10.5. String Literals says A string literal consists of zero or more characters enclosed in double quotes. And, then includes this example
The following are examples of string literals:
"" // the empty string
As Java String(s) are immutable, it has a constant length of 0 and stores a sequence of 0 characters.
When you initialize it with null like this:
String s1 = null;
It means that no object is assigned to variable s1. However, when you initialize it with empty String like this:
String s2 = "";
It means that a String object is assigned to s2 (although empty).
Now if you want to perform any operation, let's say you want to call the .equals(..) method of string, then it will throw NullPointerException in the former case as there is not String object. However, it will work fine in latter because there is an object there, it doesn't matter whether it's empty or not
s1.equals("something"); // throws NullPointerException
s2.equals("something"); // works fine
This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 4 years ago.
Currently attempting to split a large string field into 3 smaller fields. The string delimited by a "/". Example String:
0123/ABCD1234/EFGH909883432212
At the moment I have managed to pull the middle section out using the following expression inside a variable:
$F{String}.split("/" ,5)[1].trim()
To be perfectly honest I am not sure how it works as I do not know what the 5 and 1 are for (which is probably what I need to know to get the other two sections)
After calling method spit an array is created holding substrings which are delimited by "/" in the original string. The trim removes any trailing spaces.
Number 5 resembles optional parameter.
An integer that specifies the number of splits, items after the split limit will not be included in the array.
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.