Java Split String and Combine - java

I would like to split a string and combine them.
String value = "1,A 2,B 3,C"
outputs
[1,2 A,B]
[1,3 A,C]
[2,3 B,C]
If I do String[] tokens = value.split("[,\\s]+");
tokens[0] = "1" tokens[1] = "A" tokens[2] = "2" tokens[3] = "B" and so on.
But then how can I combine it that becomes the output? Thank you.

You can split and combine it by doing this:
String a = value.charAt(0)+","+value.charAt(4)+" "+value.charAt(2)+","+value.charAt(6);
String b = value.charAt(0)+","+value.charAt(8)+" "+value.charAt(2)+","+value.charAt(10);
String c = value.charAt(4)+","+value.charAt(8)+" "+value.charAt(6)+","+value.charAt(10);

Related

split an already split array string in Java

I have a csv that has been read and split in 3 different csvs. The csv was pipe separated and the split variable is saved in a string variable. I want to split the new string as comma separated string but as soon as I do that, it gives an exception.`
try(BufferedReader br1 = new BufferedReader(new FileReader(newcsvCategory))){
String line;
while ((line = br1.readLine()) != null) {
String[] value1 = line.split("\\|,",-1);
String Id = value1[0];
String CatId=value1[1];
["Active Catalog Detail (Network Id "|" Category Ids "]
["209"|"4900,10368,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,10342,5193,10738,11744,10039,10840,5132,10011,11132,5233,10792"]
["174"|"4900,10082,10119,10358,10039,5132,10011"]
["200"|"4900,10368,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,5193,10738,11623,10039,10840,5132,10011,11132,5233,10792"]
["181"|"4900,10358,10011"]
["240"|"4900,10368,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,10342,5193,10738,11744,10039,10840,5132,10011,11132,5233,10792"]
["206"|"4900,10368,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,5193,10738,11623,10039,10840,5132,10011,11132,5233,10792"]
["255"|"4900,10368,11093,11581,10082,10206,11621,10431,10119,11622,10358,11094,2,10342,5193,10738,11744,10039,10840,5132,10011,11132,5233,10792"]
["251"|"4900,10368,11093,11581,10082,10206,11621,10431,10119,11622,10358,11094,2,10342,5193,10738,11744,10039,10840,5132,10011,11132,5233,10792"]
["231"|"4900,10368,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,10342,5193,10738,11744,10039,10840,5132,10011,11132,5233,10792"]
["179"|"4900,10368,11618,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,5193,10738,11623,10039,10840,5132,10011,11132,5233,10792"]
["184"|"4900,10368,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,5193,10738,11623,10039,10840,5132,10011,11132,5233,10792"]
["187"|"4900,10368,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,5193,10738,11623,10039,10840,5132,10011,11132,5233,10792"]
["247"|"4900,10368,11093,11581,10082,10206,11621,10431,10119,11622,10358,11094,2,10342,5193,10738,11744,10039,10840,5132,10011,11132,5233,10792"]
["215"|"10358"]
["216"|"4900,10368,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,10342,5193,10738,11744,10039,10840,5132,10011,11132,5233,10792"]
["238"|"4900,10368,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,10342,5193,10738,11744,10039,10840,5132,10011,11132,5233,10792"]
["224"|"4900,10368,11093,11581,10082,10206,10431,10119,11622,10358,11094,2,10342,5193,10738,11744,10039,10840,5132,10011,11132,5233,10792"]
I want split the first column and second column as pipe separated and then further separate the second column as comma separated.
I'd appreciate any help as I'm a newbie.
added code that is splitting CatId:
String[] temp = CatId.split(",",-1);
System.out.println(temp[1]);
Really, can't realise the questions, but give some notes.
// this source string: serveral columsn with different separators
String str = "209|4900,10368,11093,11581";
According to your code, you try to put all separate number into string array, with two steps:
String[] arr = str.split("\\|"); // not line.split("\\|,",-1)
// arr[0] = 209
// arr[1] = [4900,10368,11093,11581]
String[] tmp = arr[1].split(",")
// tmp[0] = 4900
// tmp[1] = 10368
// tmp[2] = 11093
// tmp[3] = 11581
If so, you can do it with one step:
String[] arr = str.split("[\\|,]");
// arr[0] = 209
// arr[1] = 4900
// arr[2] = 10368
// arr[3] = 11093
// arr[4] = 11581
You want to set the Limit of .split(..) to 2.
while ((line = br1.readLine()) != null) {
String[] value1 = line.split("\\|",2);
String Id = value1[0];
String CatId=value1[1]
};
To further split the contet of "CatId" use:
// if you need to replace unwanted chars first, you could just use the simple .replace:
CatId = CatId.replace("\"", "").replace("[", "").replace("]", "");
// Then, split the array just by ,
String[] catIdArray = CatId.split("\\,");

regex to match and replace two characters between string

I have a string String a = "(3e4+2e2)sin(30)"; and i want to show it as a = "(3e4+2e2)*sin(30)";
I am not able to write a regular expression for this.
Try this replaceAll:
a = a.replaceAll("\) *(\\w+)", ")*$1");
You can go with this
String func = "sin";// or any function you want like cos.
String a = "(3e4+2e2)sin(30)";
a = a.replaceAll("[)]" + func, ")*siz");
System.out.println(a);
this should work
a = a.replaceAll("\\)(\\s)*([^*+/-])", ") * $2");
String input = "(3e4+2e2)sin(30)".replaceAll("(\\(.+?\\))(.+)", "$1*$2"); //(3e4+2e2)*sin(30)
Assuming the characters within the first parenthesis will always be in similar pattern, you can split this string into two at the position where you would like to insert the character and then form the final string by appending the first half of the string, new character and second half of the string.
string a = "(3e4+2e2)sin(30)";
string[] splitArray1 = Regex.Split(a, #"^\(\w+[+]\w+\)");
string[] splitArray2 = Regex.Split(a, #"\w+\([0-9]+\)$");
string updatedInput = splitArray2[0] + "*" + splitArray1[1];
Console.WriteLine("Input = {0} Output = {1}", a, updatedInput);
I did not try but the following should work
String a = "(3e4+2e2)sin(30)";
a = a.replaceAll("[)](\\w+)", ")*$1");
System.out.println(a);

Usage of the java function 'split()

I would like to split the word in java based on the delimeter'-'when it appeared last.
I am expecting the result as "sweet_memory_in" and "everbodylife#gmail.com". Do we have any inbuilt function in java.
Complete word is sweet_memory_in_everbodylife#gmail.com
String s = "sweet_memory_in_everbodylife#gmail.com";
String first = s.substring(0,s.lastIndexOf("_"));
String second = s.substring(s.lastIndexOf("_")+1 );
try this
String s = "sweet_memory_in_everbodylife#gmail.com";
String s1 = s.substring(0,s.lastIndexOf("_"));
String s2 = s.substring(s.lastIndexOf("_")+1,s.length());
Regex may help. Other way is to get the last index of _ and use substring to split it.
Try out this code :
String data = "sweet_memory_in_everbodylife#gmail.com";
int lastIndex = data.lastIndexOf("_");
String firstSplit = data.substring(0, lastIndex);
String secondSplit = data.substring(lastIndex + 1, data.length());
System.out.println(firstSplit);
System.out.println(secondSplit);
Try this my friend (Javascript Codes):
var str = 'sweet_memory_in_everbodylife#gmail.com';
var arr1 = str.substring(str.lastIndexOf("_")).split("_");
var arr2 = str.split("_"+arr1[1]);
alert(arr2[0] +" --> "+arr1[1]);

Cut ':' && " " from a String with a tokenizer

right now I am a little bit confused. I want to manipulate this string with a tokenizer:
Bob:23456:12345 Carl:09876:54321
However, I use a Tokenizer, but when I try:
String signature1 = tok.nextToken(":");
tok.nextToken(" ")
I get:
12345 Carl
However I want to have the first int and the second int into a var.
Any ideas?
You have two different patterns, maybe you should handle both separated.
Fist you should split the space separated values. Only use the string split(" "). That will return a String[].
Then for each String use tokenizer.
I believe will works.
Code:
String input = "Bob:23456:12345 Carl:09876:54321";
String[] words = input.split(" ")
for (String word : words) {
String[] token = each.split(":");
String name = token[0];
int value0 = Integer.parseInt(token[1]);
int value1 = Integer.parseInt(token[2]);
}
Following code should do:
String input = "Bob:23456:12345 Carl:09876:54321";
StringTokenizer st = new StringTokenizer(input, ": ");
while(st.hasMoreTokens())
{
String name = st.nextToken();
String val1 = st.nextToken();
String val2 = st.nextToken();
}
Seeing as you have multiple patterns, you cannot handle them with only one tokenizer.
You need to first split it based on whitespace, then split based on the colon.
Something like this should help:
String[] s = "Bob:23456:12345 Carl:09876:54321".split(" ");
System.out.println(Arrays.toString(s ));
String[] so = s[0].split(":", 2);
System.out.println(Arrays.toString(so));
And you'd get this:
[Bob:23456:12345, Carl:09876:54321]
[Bob, 23456:12345]
If you must use tokeniser then I tink you need to use it twice
String str = "Bob:23456:12345 Carl:09876:54321";
StringTokenizer spaceTokenizer = new StringTokenizer(str, " ");
while (spaceTokenizer.hasMoreTokens()) {
StringTokenizer colonTokenizer = new StringTokenizer(spaceTokenizer.nextToken(), ":");
colonTokenizer.nextToken();//to igore Bob and Carl
while (colonTokenizer.hasMoreTokens()) {
System.out.println(colonTokenizer.nextToken());
}
}
outputs
23456
12345
09876
54321
Personally though I would not use tokenizer here and use Claudio's answer which splits the strings.

Parse string starting from the end

I want to split string around only the last _ character , example:some_string_foo_bar into two substrings some_string_foo bar.
I tried to use Pattern and StringTokenizer, but they always start from the beginning of stirng. Any idea how to do this?
Use lastIndexOf; there's no reason to do a full split.
Sure, this might be of some use. Here's an example.
String source = "hello_world_foo";
int pos = source.lastIndexOf('_');
String before = source.substring(0, pos);
String after = source.substring(pos + 1);
You can use:
String strX = "some_string_foo";
String str1 = strX.substring(0,strX.lastIndexOf("_"));
String str2 = strX.subscting(strX.lastIndexOf("_"),strX.length());
String[] arr = str.split("_");
String lastOne = arr[arr.length-1];

Categories