split string and set values to class variable in java 8 [duplicate] - java

This question already has answers here:
Splitting a Java String by the pipe symbol using split("|")
(7 answers)
Closed 2 years ago.
String decodedChecksum="A01046085|T98494055e|1200|2020-05-31T06:12:46.365Z"
String[] splitArray = decodedChecksum.split("|");
/* here i want to set values to getter setter*/
{
sample.setAppNo(A01046085)
sample.setId(T98494055e)
..
}
Please provide solution to iterate the array and set valuease to varibale

It heavily depends on how your code is structured. If you're 100% sure of the structure of that string, it's as easy as:
String decodedChecksum="A01046085|T98494055e|1200|2020-05-31T06:12:46.365Z"
String[] splitArray = decodedChecksum.split("\\|");
sample.setAppNo(splitArray[0]);
sample.setId(splitArray[1]);
...

You need to escape the | because it's a special symbol in regex.
String decodedChecksum="A01046085|T98494055e|1200|2020-05-31T06:12:46.365Z";
String[] splitArray = decodedChecksum.split("\\|");
sample.setAppNo(splitArray[0]); // splitArray[0] = A01046085
sample.setId(splitArray[1]); // splitArray[0] = T98494055e
...

Related

getting the string value before dot in string through java [duplicate]

This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 7 years ago.
I have a string named extractDb values can be
GDSHKG.db
GDSMNH.db
GDSTKY.db
GDSLDN.db
now i want to remove the .extension partof it
that is i want to see the result as
GDSHKG
GDSMNH
GDSTKY
GDSLDN
please advise how to achieve this in java
Try to use substring like this:
String myString = "GDSHKG.db";
String newString = myString.substring(0, myString.indexOf("."));
System.out.print(newString);
Output:
GDSHKG

How to print only two digits after decimal with System.printf? [duplicate]

This question already has answers here:
String.split(".") is not splitting my long String
(2 answers)
Closed 8 years ago.
My problem is that double value converted to string cannot be splitted by dot.
Here you can see my code:
String valueOf = String.valueOf(12.34);
System.out.println("valueOf=" + valueOf);
String[] split = valueOf.split(".");
System.out.println("split=" + Arrays.toString(split));
The output is:
valueOf = 12.34
split = []
Why is split array empty?
You can try to run it on https://ideone.com/BBL4z2.
You need to escape . here. you can use \\.
String[] split = valueOf.split("\\.");
Because in regex you need to escape .

Sub string from the String [duplicate]

This question already has answers here:
String.Split with a String?
(2 answers)
Closed 9 years ago.
I have a string "552 s hello"
I want to get the "552" in one substring and "s" in other substring and I want to split this String on the basis of first two spaces.Please help...
You can do it like this:
String t = "522 s hello"
String[] test = t.split(" ");
Now in the String array you will have three values, "522", "s" and "hello. And you can now access each.
Use String#split(String regex):
String[] sp="552 s hello".split(" ");
String fiveFiveTwo=sp[0];
String letterS=sp[1];
The names aren't very useful in the end, so you'll want to rename the strings I've created. You should also catch ArrayIndexOutOfBoundsException.

Java split function [duplicate]

This question already has answers here:
split string on comma, but not commas inside parenthesis
(3 answers)
Closed 9 years ago.
Hi I have a question about java split
ItemName,Item,,(width,length,height),12
I want String[] equals this"
String[] result ={"ItemName","Item","","(width,length,height)","12"};
Who can help me with using split function or other regex?
If your input string is named istr, then
String[] result = istr.split(",");
should work.
try this
String data="ItemName,Item,,(width,length,height),12";
String[] result=data.split("//,");
for(String string : result) {
System.out.println(string);
}

Java - simple string issue [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
string split in java
I have a string:
"jack#james#joahn#logan#"
I know there is a method of the String class that can seperate it on the symbol #. So it will return to me
"jack" "james" "joahn" "logan"
What is the name of this method?
This is String.split("#").
For example:
String s = "jack#james#joahn#logan#";
String parts[] = s.split("#");
See the String.split method. Here is an example:

Categories

Resources