Extract/Trim part of string, Java [duplicate] - java

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

Related

How to check if a character is inside a string or number in java [duplicate]

This question already has answers here:
How can I check if a single character appears in a string?
(16 answers)
How do I convert from int to String?
(20 answers)
How to check if a number contains a certain digit?
(3 answers)
Closed 2 years ago.
I tried to make a function that takes one argument (a number) and returns true/false if it has 6 inside of it. I know how to do that in python.
In python it should look like this:
def six(num):
num=str(num)
return "6" in num
How do I do it in Java?
public boolean check(int x) {
return Integer.toString(x).contains("6");
}

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

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

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

Removing unneeded zeroes [duplicate]

This question already has answers here:
Remove trailing zero in Java
(12 answers)
Closed 7 years ago.
How to delete zeroes in java (android) only if there is no non-zero value following them?
The value is stored in double and then later in string, so working on these variables would be best.
Example:
I have 12.50000
I want to have 12.5
Example2:
I have 65.4030
I want to have 65.403
Try this :
String s = 12.50000;
s = s.indexOf(".") < 0 ? s : s.replaceAll("0*$", "").replaceAll("\\.$", "");

Integer - toString() function [duplicate]

This question already has answers here:
How to convert number to words in java
(31 answers)
Int to English words in Java
(4 answers)
Closed 9 years ago.
I search but cannot find on the web.
Is there a function that can represent any integer into its String litteral representation ?
Example :
Integer i1 = new Integer(4);
Integer i2 = new Integer(30);
i1.callSomeFunction();
i2.callSomeFunction();
Output :
"Four"
"Thirty"
If you know another function which isn't in the Integer class, it's fine too.
There is no standard method to do this but take a look at this. There is an example for a number to words in the English language converter.

Categories