This question already has answers here:
Difference between int[] array and int array[]
(26 answers)
Closed 8 years ago.
I have a simply doubt about syntax in Java related with Strings.
Could some one tell me what is the difference between the strings in the following code?
String[] firstS = {"word1","word2"};
String secondS[] = {"word1","word2"};
I can not realize what is the difference because doing some for loop both String have the same output.
Thank you so much.
There is no difference, but the first is often considered better style.
Related
This question already has answers here:
Is there a Java equivalent to Python's Easy String Splicing?
(8 answers)
Closed 2 years ago.
I am trying to figure out how to return a selected amount of characters in my java string.
In Python, it is just like
randomVar = "hello"
print(randomVar[1:])
and the output would be "ello".
How do I do the same thing in java?
Thank you!
This can be accomplished with substring()
String a = "hello";
System.out.println(a.substring(1, a.length());
This question already has answers here:
Difference between int[] array and int array[]
(26 answers)
Closed 5 years ago.
Like int [] i or int i [] is there any difference
I'm curious
No, int[] i is the same as int i[], but the latter one is discouraged.
See the official documentation on Arrays:
However, convention discourages this form; the brackets identify the array type and should appear with the type designation.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
I already tried with string.equal function which will compare two strings, but what will be optimize way of doing same.
Suggestions are welcome.
Hi all got it may help others What's the quickest way to compare strings in Java?
Thanks Saurabh for sharing link.
According to me, You should use the equals() method of the String class to compare Strings.
if (xyz.equals("something")) {
//Do what you want if string is equal...
}
see this also..
This question already has answers here:
Java loop efficiency ("for" vs. "foreach") [duplicate]
(5 answers)
Closed 8 years ago.
What is very faster, normal {for loop} or {foreach loop} in java ?
They are different first of all. for-each uses Iterator under the hood whereas for is useful for operations on arrays or where you can do something meaningful with indexes.
This question already has answers here:
How can I check if a single character appears in a string?
(16 answers)
Closed 9 years ago.
I have a string like :
"\"[\"a\",\"b\", \"c\"]\""
How to convert this to a list of strings
Could you suggest me a nice way of doing it in Java?
str.contains("c") does this job. However did not you think to consult String class documentation first?
Use contains():
if (str.contains("\"c\""))