This question already has answers here:
split string at index
(5 answers)
Closed 2 years ago.
I get a number generated as a string that always has the same length, for example:
0107612733631449211907028445
Now I wan't to get two separate strings with the last 10 digits:
010761273363144921**1907028445**
And another string with these digits
0107612733**63144**9211907028445
The positions of these needed numbers are always the same, the numbers at the beginning are not important for me and can be omitted.
How can I get these two strings?
Java docs .substring method
String number = "0107612733631449211907028445";
String last10 = number.substring(number.length()-10);
for middle numbers use substring(int beginIndex, int endIndex)
Related
This question already has answers here:
How to remove the last character from a string?
(37 answers)
Closed 4 years ago.
I have a string oCustomerOrderNumber which I am passing a value into iCustomerOrderNumber. I would like to remove the last 3 characters from this iCustomerOrderNumber but unable to get this to work. Error I get is that it does not like the -.
Code:
oCustomerOrderNumber = iCustomerOrderNumber.trim() -
(iCustomerOrderNumber.length() -3);
Example. ICustomerOrderNumber is 1234567 I want oCustomerOrderNumber to be 1234.
For getting a portion of a string you can use substring method and
here you can define from where until where you want to fetch from any
String:
iCustomerOrderNumber.substring(0, iCustomerOrderNumber.length()-3);
oCustomerOrderNumber = iCustomerOrderNumber.substring(0, (iCustomerOrderNumber.length() -3));
This question already has answers here:
How to evaluate a math expression given in string form?
(26 answers)
Closed 6 years ago.
I want to do calculator that do sums but just in one index like this:
args[0] = "2+2"
and the output should be 4.
I won't do your homework for you, but I'll split it in few steps:
You should get String before the "+" and after the "+":
Have a look at String.indexOf and String.substring for that. Make sure you check for -1 for indexOf.
Parse String to int:
int foo = Integer.parseInt(String s);
Now you can add two ints.
Good luck!
This question already has answers here:
Java String split removed empty values
(5 answers)
Closed 6 years ago.
In Java:
String base = "a|a||";
String[] stri= .split("\\|");
produces a string array with length 2.
On the other hand in python:
base = "a|a||"
base.split("|")
produces an array with length 4.
What do I have to do to get the same result in Java?
Use split with limit set to negative value:
String base = "a|a||";
String[] stri= .split("\\|", -1);
From the docs (the number at the and is n):
If n is non-positive then the pattern will be applied as many times as possible and the array can have any length.
This question already has answers here:
Why should a Java class implement comparable?
(10 answers)
Closed 6 years ago.
String ntext;
ntext = something;
String currentLine;
currentLine = something;
while(ntext.compareTo(currentLine) != 0){
//some condition
}
Here i want to know what that compareto actually do.
One more questin what we can use to compare two objects?
If those variables are strings (i assume so) it checks if they are equal, returns 0 if so, and another number if not. See the JavaDoc here: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#compareTo%28java.lang.String%29
More specifically, it goes through both strings character by character. When it finds a string of one that is not equal to the other, it returns a number representing whether the differing character is more than, less than, or equal to the corresponding character in the other string.
This question already has an answer here:
Arrays with trailing commas inside an array initializer in Java
(1 answer)
Closed 7 years ago.
int [][] a = {{1,2,},{3,4}};
int [][] a = {{1,2},{3,4}};
i have two Multidimensional arrays in java. I want to know what's the exact difference in both of them.just see there is a comma after 2 in first one.
JLS 10.6. Array Initializers: A trailing comma may appear after the last expression in an array initializer and is ignored.
There is no difference. The extra comma might be useful if you are formatting your elements one per line, so that every element appears the same.