How to count comma in a String java [duplicate] - java

This question already has answers here:
How do I count the number of occurrences of a char in a String?
(48 answers)
Closed 2 years ago.
I have a String, which contains a lot of commas.
I want to count all the commas in the String but I don't know how.
I'm using (split(",")-1)
But the problem is that if I input a String like this: One,Two,Three,Four,,,
Then it returns only 3 while I want it to be 6.
I think it is because split(",") returns a String[] that does not include null or empty values.
Is there any way to solve this problem?

One straightforward way would be to just compare the length of the input against the input with all commas removed:
String input = "One,Two,Three,Four,,,";
int numCommas = input.length() - input.replace(",", "").length();
System.out.println(numCommas); // prints 6

Related

How to take 2 digits number in string and convert it to int [duplicate]

This question already has answers here:
Splitting and converting String to int
(5 answers)
Closed 3 years ago.
String code = "U 12 24";
int s = Integer.parseInt(String.valueOf(code.charAt(2)));
System.out.println(s);
that would be print 1,
however, i want to try print 12 or i mean i want take 2 digits number, but i can't do it because the only way i know is just take one digit number.
how if i want take 12 and convert to int
int s = Integer.parseInt(String.valueOf(code.substring(2, 4)));
If you want to get all digits in a given string, you have to tokenize the string by space and parse every chunk into a number.

How can I Split the String Correctly? [duplicate]

This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 3 years ago.
I am getting a result of "kjhvkjkasdfkjh#grouping#group_group.12018-08-20".
I am looking to split the string in java to only get the "2018-08-20".
Any suggestions? The part I want is always at the end.
String sample = "kjhvkjkasdfkjh#grouping#group_group.12018-08-20"
int SubstringStart = sample.length() - 10;
String outputsample = sample.substring(SubstringStart);
will break if string has length shorter than 10

Java String.format() leading zeros and 1 decimal place [duplicate]

This question already has answers here:
Add leading zeroes to number in Java? [duplicate]
(5 answers)
Closed 4 years ago.
I need a string like 50 to appear as 050.0. I am using String.format, but I can't figure out how to do leading zeros and a single decimal place at the same time. So far, I have tried String.format("%3.2f", number);, but that isn't working as I still get 50.0 rather than 050.0
Use DecimalFormat to control the number of mandatory digits:
DecimalFormat df = new DecimalFormat("#000.0");
System.out.println(df.format(50)); // 050.0
where
Symbol Location Localized? Meaning
0 Number Yes Digit
# Number Yes Digit, zero shows as absent
You can use StringBuilder class to create a string with number 0 and then append it with you number and insert the decimals at the end.
int num = 50; /*Your number*/
StringBuilder s_num = new StringBuilder("0");
s_num.append(num);
s_num.append(".0");
String f_num = s_num.toString();

How to increment the string value? [duplicate]

This question already has answers here:
How to increment the number in a String by 1?
(7 answers)
Closed 5 years ago.
For example:
public static void main(String[] args)
{
String a="1";
int inc= Integer.parseInt(a+1);
System.out.println(inc);
}
I'm getting 11 but i want to get 2. How can i do it in a very efficient way?
Integer.parseInt(a+1); parses the String that results from concatenating the value of the String a ("1") to the int literal 1, which is "11".
Change it to
int inc = Integer.parseInt(a) + 1;
This way "a" would be parsed to the integer 1 and then 1 would be added to it to give you the value 2.
Since a is a String object this operation is not giving the desired input
Integer.parseInt(a+1);
because will be equivalent to do
Integer.parseInt("1"+"1");
or
Integer.parseInt("11");
you need to parse the string first and then increment
Integer.parseInt(a)+1

Sum of numbers of a number [duplicate]

This question already has answers here:
How to sum digits of an integer in java?
(22 answers)
Closed 6 years ago.
Say I have the number 123
Is there a computationally efficient way of adding 1+2+3 and getting the answer back
Ideally I'd avoid division or parsing to String as I feel this can be quite inefficient.
No need of parsing to string.
int sum=0;
while(n>0)
{
sum += n%10; // add the last digit
n/=10; // remove the last digit.
}

Categories