Java: String splitting into multiple elements - java

I am having a difficult time figuring out how to split a string like the one following:
String str = "hi=bye,hello,goodbye,pickle,noodle
This string was read from a text file and I need to split the string into each element between the commas. So I would need to split each element into their own string no matter what the text file reads. Keep in mind, each element could be any length and there could be any amount of elements which 'hi' is equal to. Any ideas? Thanks!

use split!
String[] set=str.split(",");
then access each string as you need from set[...] (so lets say you want the 3rd string, you would say: set[2]).
As a test, you can print them all out:
for(int i=0; i<set.length;i++){
System.out.println(set[i]);
}

If you need a bit more advanced approach, I suggest guava's Splitter class:
Iterable<String> split = Splitter.on(',')
.omitEmptyStrings()
.trimResults()
.split(" bye,hello,goodbye,, , pickle, noodle ");
This will get rid of leading or trailing whitespaces and omit blank matches. The class has some more cool stuff in it like splitting your String into key/value pairs.

str = str.subString(indexOf('=')+1); // remove "hi=" part
String[] set=str.split(",");

I'm wondering: Do you mean to split it as such:
"hi=bye"
"hi=hello"
"hi=goodbye"
"hi=pickle"
"hi=noodle"
Because a simple split(",") will not do this. What's the purpose of having "hi=" in your given string?
Probably, if you mean to chop hi= from the front of the string, do this instead:
String input = "hi=bye,hello,goodbye,pickle,noodle";
String hi[] = input.split(",");
hi[0] = (hi[0].split("="))[1];
for (String item : hi) {
System.out.println(item);
}

Related

How to Ignore the desired string during the split in Java?

I have a string like
pchase_history:array<struct<pchase_channel:string,trans_dt:string,sku_id:string,sold_qty:bigint>>,first_pchase_dt:string,last_pchase_dt:string,trans_cnt:bigint,last_pchase_sku_cnt:bigint,no_of_pchase_days:bigint,lst_pchase_channel:array<struct<pchase_channel:string>>
and i need to split it by ',' but don't want to split (array of struct) array<struct<pchase_channel:string,trans_dt:string,sku_id:string,sold_qty:bigint>>
I want split method to ignore these array of struct and split the rest of the string.
How can i achieve this by split method?
Any help would be appreciated.
You can use a regex to replace your array of struct before doing split like this:
String value = "pchase_history:array<struct<pchase_channel:string,trans_dt:string,sku_id:string,sold_qty:bigint>>,first_pchase_dt:string,last_pchase_dt:string,trans_cnt:bigint,last_pchase_sku_cnt:bigint,no_of_pchase_days:bigint,lst_pchase_channel:array<struct<pchase_channel:string>>";
value = value.replaceAll("(array<struct<.*?>>)", "array");
String[] splitedValues = value.split(",");
System.out.println(Arrays.toString(splitedValues));
Output:
[pchase_history:array, first_pchase_dt:string, last_pchase_dt:string, trans_cnt:bigint, last_pchase_sku_cnt:bigint, no_of_pchase_days:bigint, lst_pchase_channel:array]
Click here to test regex online

multiple sections in a csv row

I have a csv file formatted
<F,Bird,20,10/> < A,Fish,5,11,2/>
I was wondering how to read in those values separately.
Would I have to get the whole line to an array?
I have thought of doing line.split("/>") but then the first data would have < in them which I don't want.
If I on the other hand just seperate it using line.split(",") and then assign each values accordingly the values in the middle would merge so that does not work neither.
Is there a way to separate the string first without the <>/ symbols?
You can use several delimiters in split regexp, like this:
String line = "<F,Bird,20,10/> < A,Fish,5,11,2/>";
String[] lines = line.split("<|/> <|/>");
for (String item: lines) {
System.out.println(item);
}
Output (with all your spaces):
F,Bird,20,10
A,Fish,5,11,2
Try splitting your input string using the lookbehind ?<=/>:
String input = "<F,Bird,20,10/> < A,Fish,5,11,2/>";
input = input.replaceAll("\\s+", "");
String[] parts = input.split("(?<=/>)");
for (String part : parts) {
System.out.println(part.replaceAll("[<>/]", ""));
}
Note that I removed all spaces from your string to make splitting cleaner. We could still try to split with arbitrary whitespace present, but it would be more work. From this point, you can easily access the CSV data contained within each tag.
Output:
F,Bird,20,10
A,Fish,5,11,2
Demo here:
Rextester

Looking for method to remove spaces on sides, change all letters to small with first letter as capital letter

I have been trying for a while to make a method which takes an user input and changes it so that potential spaces infront and after the text should be removed. I tried .trim() but doesnt seem to work on input strings with two words. also I didnt manage to make both first and second word have the first letter as Capital.
If user inputs the following string I want all separate words to have all small letters except for the first in the word. e.g: Long Jump
so if user inputs:
"LONG JuMP"
or
" LoNg JUMP "
change it to
"Long Jump"
private String normalisera(String s) {
return s.trim().substring(0,1).toUpperCase() + s.substring(1).toLowerCase();
}
I tried the method above but didnt work with two words, only if the input was one. It should work with both
To remove all spaces extra spaces you can do something like this
string = string.trim().replaceAll(" +", " ");
The above code will call trim to get rid of the spaces at the start and end, then use regex to replace everything that has 2 or more spaces with a single space.
To capitalize the first word, if you're using Apache's commons-lang, you can use WordUtils.capitalizeFully. Otherwise, you'll need to use a homebrewed solution.
Simply iterate through the String, and if the current character is a space, mark the next character to be uppercased. Otherwise, make it lowercase.
Split your problems into smaller ones:
You need to be able to:
iterate over all words and ignore all whitespaces (you can use Scanner#next for that)
edit single word into new form (create helper method like String changeWord(String){...})
create new String which will collect edited versions of each word (you can use StringBuilder or better StringJoiner with delimiter set as one space)
So your general solution can look something like:
public static String changeWord(String word) {
//code similar to your current solution
}
public static String changeText(String text) {
StringJoiner sj = new StringJoiner(" ");// space will be delimiter
try(Scanner sc = new Scanner(text)){
while (sc.hasNext()) {
sj.add(changeWord(sc.next()));
}
}
return sj.toString();
}
Since Strings are immutable and you cannot make in place changes you need to store it in a separate variable and then do your manipulations like this:
String s = " some output ";
String sTrimmed = s.trim();
System.out.println(s);
System.out.println(sTrimmed);
Change your code like this for the rest of your code as well.

Regex Pattern to avoid : and , in the strings

I have a string which comes from the DB.
the string is something like this:-
ABC:def,ghi:jkl,hfh:fhgh,ahf:jasg
In short String:String, and it repeats for large values.
I need to parse this string to get only the words without any : or , and store each word in ArrayList
I can do it using split function(twice) but I figured out that using regex I can do it one go and get the arraylist..
String strLine="category:hello,good:bye,wel:come";
Pattern titlePattern = Pattern.compile("[a-z]");
Matcher titleMatcher = titlePattern.matcher(strLine);
int i=0;
while(titleMatcher.find())
{
i=titleMatcher.start();
System.out.println(strLine.charAt(i));
}
However it is not giving me proper results..It ends up giving me index of match found and then I need to append it which is not so logical and efficient,.
Is there any way around..
String strLine="category:hello,good:bye,wel:come";
String a[] = strLine.split("[,:]");
for(String s :a)
System.out.println(s);
Use java StringTokenizer
Sample:
StringTokenizer st = new StringTokenizer(in, ":,");
while(st.hasMoreTokens())
System.out.println(st.nextToken());
Even if you can use a regular expression to parse the entire string at once, I think it would be less readable than splitting it with multiple steps.

How do I fill a new array with split pieces from an existing one? (Java)

I'm trying to split paragraphs of information from an array into a new one which is broken into individual words. I know that I need to use the String[] split(String regex), but I can't get this to output right.
What am I doing wrong?
(assume that sentences[i] is the existing array)
String phrase = sentences[i];
String[] sentencesArray = phrase.split("");
System.out.println(sentencesArray[i]);
Thanks!
It might be just the console output going wrong. Try replacing the last line by
System.out.println(java.util.Arrays.toString(sentencesArray));
The empty-string argument to phrase.split("") is suspect too. Try passing a word boundary:
phrase.split("\\b");
You are using an empty expression for splitting, try phrase.split(" ") and work from there.
This does nothing useful:
String[] sentencesArray = phrase.split("");
you're splitting on empty string and it will return an array of the individual characters in the string, starting with an empty string.
It's hard to tell from your question/code what you're trying to do but if you want to split on words you need something like:
private static final Pattern SPC = Pattern.compile("\\s+");
.
.
String[] words = SPC.split(phrase);
The regex will split on one or more spaces which is probably what you want.
String[] sentencesArray = phrase.split("");
The regex based on which the phrase needs to be split up is nothing here. If you wish to split it based on a space character, use:
String[] sentencesArray = phrase.split(" ");
// ^ Give this space

Categories