How can I Split the String Correctly? [duplicate] - java

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

Related

concatenation and displaying a string [duplicate]

This question already has answers here:
Does concatenating strings in Java always lead to new strings being created in memory?
(3 answers)
Converting String to "Character" array in Java
(14 answers)
String to char array Java
(1 answer)
Closed 3 months ago.
a string created with String class can not be modified.
but when we use the += operator does it mean that the original string change?
exp:
String ch="hello"; ch+= "world";
another question:
why we don't use these instructions to display the string ?
for (int i=0;i<ch.length();i++) {System.out.println(ch[i]); }
i tried this
for (int i=0;i<ch.length();i++) {System.out.println(ch.charAt(i)); }
why it is not similar to
for (int i=0;i<ch.length();i++) {System.out.println(ch[i]); }

How to count comma in a String java [duplicate]

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

I want to store date in dd-MMM-yyyy (17-MAR-2017) in string say X. [duplicate]

This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 5 years ago.
in one string say X. Now I want to split this string in another string that is, say y= x.split , such that y[0] = 17, y[1] = MAR y[2] = 2017. How to write prog for this in Java.
Try splitting:-
String s = "17-MAR-2017";
String array[] = s.split("-");
System.out.println(array[0]); //Prints 17. Same way, print others.

How string concat method works [duplicate]

This question already has answers here:
String is immutable. What exactly is the meaning? [duplicate]
(19 answers)
Closed 6 years ago.
I know String in Java is immutable. Consider:
String s = "Java";
s = s.concat(" is simple");
System.out.println(s); //prints Java is simple
Here the value of String s is changed? Can someone explain me, how String concat actually works?
s.concat(" is simple") returns a new string and s = s.concat(" is simple") assigns this new string to variable s.

Can you somehow turn a String like "14232*12331-1231" into something Java can Calculate? [duplicate]

This question already has answers here:
How to evaluate a math expression given in string form?
(26 answers)
Closed 8 years ago.
I'd like to know if you can somehow get something like
String test= "3445+100";
double x= test
so I can turn a typed in calculation into a result?
First time asking sorry if I waste your time :/
You split the String between the operators. Then you cast the Number-Strings to int and you can cast the Operators with a distinction of cases because you can have only +,*,- and /.
String test= "3445+100";
String[] numberArray=test.split("\\+");
int a = Integer.parseInt(numberArray[0]);
int b = Integer.parseInt(numberArray[1]);
int solution=a+b;

Categories