Minusing 3 characters from the end of a string in Java [duplicate] - java

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));

Related

String format query [duplicate]

This question already has answers here:
How can I pad an integer with zeros on the left?
(18 answers)
Closed 5 years ago.
What does it represents in java?
String.format("%01000d",0)
It prints 0 thousands times. But can anyone help how does it actually works. What does "%01000d" represents?
The first argument is a format string, here you can find the sintax:
https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax

Extract/Trim part of string, Java [duplicate]

This question already has answers here:
Java - removing first character of a string
(14 answers)
Closed 5 years ago.
Say String x = MynameisMary;
I want String Y to be "Mary"
How do i remove the first 8 characters to make Y equal to Mary??
You could use the substring method:
String result = x.substring(8);

Calculate a formula from a string [duplicate]

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!

How do I get the same results with Java split as with Python split [duplicate]

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.

when converting a string to a string array during splitting, it produces an undesired output [duplicate]

This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Why does the toString method in java not seem to work for an array
(9 answers)
Closed 9 years ago.
When i try to split a String "Element" into a String array, and then print out the result i get an output which i don't understand. My code is as follows:
String element = mapArray.get(i);
elementSplit = element.split("(?!^)");
System.out.println(elementSplit);
And the output produced when i print the String Array is:
[Ljava.lang.String;#3dee2310
Could someone please advise, as i do not know why it is printing this output.
Thanks very much
You have to use Arrays.toString method.
System.out.println(Arrays.toString(elementSplit));
Due to speed You should use toCharArray method instead of split("(?!^)") and in order to print array you should use Arrays.toString method
String element = mapArray.get(i);
elementSplit = element.toCharArray();
System.out.println(Arrays.toString(elementSplit));

Categories