Java string slice | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Java string slice

please i have been try to slice a string on java but the statement are not correct can anyone tell me where i have made a mistake. var name = "caleb"; var cutName = name.slice(0,1); System.out.println(cutName);

4th Mar 2024, 1:13 PM
Caleb💯
Caleb💯 - avatar
7 Answers
+ 2
Java String does not have a slice() method. You can use substring() instead. You can check the list of all available String methods in the documentation. https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/String.html
4th Mar 2024, 1:21 PM
Tibor Santa
Tibor Santa - avatar
+ 2
Javascript does not have System.out.println :) Please make up your mind which language you want to code in 😅 And attach a link to your code for testing.
4th Mar 2024, 1:28 PM
Tibor Santa
Tibor Santa - avatar
+ 1
sorry i meant javascript
4th Mar 2024, 1:25 PM
Caleb💯
Caleb💯 - avatar
+ 1
Arham Sami actually, Java does have var keyword. It is called type inference. The compiler will try to guess the type of the variable, if it can be decided from the value of the assignment.
5th Mar 2024, 8:19 PM
Tibor Santa
Tibor Santa - avatar
+ 1
Thanks, I didn't knew I thought Java was statically typed
5th Mar 2024, 9:05 PM
Arham Sami
Arham Sami - avatar
0
Java doesn't have "var" Keyword and slice method
5th Mar 2024, 5:29 PM
Arham Sami
Arham Sami - avatar
0
Yes it is still statically typed. Type inference happens during compilation. And there are plenty of restrictions for "var". you cannot use it in function signatures or fields, only for local variables. And use must use an assignment immediately, you cannot declare a variable with "var" type not giving it a value. So it's not exactly the same as in Javascript.
6th Mar 2024, 3:30 AM
Tibor Santa
Tibor Santa - avatar