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("\\,");
I need to split a String read in from a file into an array of values. I want to split the String at the commas, so for example, if the String read:
"name, 2012, 2017"
The values in the array would be:
array index 0 - name
array index 1 - 2012
array index 2 - 2017
I found this example in Java:
String[] stringArray = string.split(",");
How I could do it in Kotlin?
val strs = "name, 2012, 2017".split(",").toTypedArray()
If we have a string of values that splited by any character like ",":
val values = "Name1 ,Name2, Name3" // Read List from somewhere
val lstValues: List<String> = values.split(",").map { it -> it.trim() }
lstValues.forEach { it ->
Log.i("Values", "value=$it")
//Do Something
}
It's better to use trim() to delete spaces around strings if exist.
Consider that if have a "," at the end of string it makes one null item, so can check it with this code before split :
if ( values.endsWith(",") )
values = values.substring(0, values.length - 1)
if you want to convert list to Array ,use this code:
var arr = lstValues.toTypedArray()
arr.forEach { Log.i("ArrayItem", " Array item=" + it ) }
Simple as it is:
val string: String = "leo_Ana_John"
val yourArray: List<String> = string.split("_")
you get: yourArray[0] == leo, yourArray[1] == Ana, yourArray[2]==John
In this case, just change the "_" from my code to ", " of yours. Se bellow
val yourArray: List<String> = string.split(", ")
var newStrg= "853 kB"
val mString = newStrg!!.split(" ").toTypedArray()
Here Split parameter is space
mString[0] = "853"
mString[1] = "kB"
Split a string using inbuilt split method then using method extensions isNum() to return numeric or not.
fun String.isNum(): Boolean{
var num:Int? = this.trim().toIntOrNull()
return if (num != null) true else false
}
for (x in "name, 2012, 2017".split(",")) {
println(x.isNum())
}
If you want to use multiple/several delimiters in kotlin split, you need to pass them separately:
val validUrl = "http://test.com/</a> -".split(">", " ", "<").first()
I am writing a small programming language for a game I am making, this language will be for allowing users to define their own spells for the wizard entity outside the internal game code. I have the language written down, but I'm not entirely sure how to change a string like
setSpellName("Fireball")
setSplashDamage(32,5)
into an array which would have the method name and the arguments after it, like
{"setSpellName","Fireball"}
{"setSplashDamage","32","5"}
How could I do this using java's String.split or string regex's?
Thanks in advance.
Since you're only interested in the function name and parameters I'd suggest scanning up to the first instance of ( and then to the last ) for the params, as so.
String input = "setSpellName(\"Fireball\")";
String functionName = input.substring(0, input.indexOf('('));
String[] params = input.substring(input.indexOf(')'), input.length - 1).split(",");
To capture the String
setSpellName("Fireball")
Do something like this:
String[] line = argument.split("(");
Gets you "setSpellName" at line[0] and "Fireball") at line[1]
Get rid of the last parentheses like this
line[1].replaceAll(")", " ").trim();
Build your JSON with the two "cleaned" Strings.
There's probably a better way with Regex, but this is the quick and dirty way.
With String.indexOf() and String.substring(), you can parse out the function and parameters. Once you parse them out, apply the quotes are around each of them. Then combine them all back together delimited by commas and wrapped in curly braces.
public static void main(String[] args) throws Exception {
List<String> commands = new ArrayList() {{
add("setSpellName(\"Fireball\")");
add("setSplashDamage(32,5)");
}};
for (String command : commands) {
int openParen = command.indexOf("(");
String function = String.format("\"%s\"", command.substring(0, openParen));
String[] parameters = command.substring(openParen + 1, command.indexOf(")")).split(",");
for (int i = 0; i < parameters.length; i++) {
// Surround parameter with double quotes
if (!parameters[i].startsWith("\"")) {
parameters[i] = String.format("\"%s\"", parameters[i]);
}
}
String combine = String.format("{%s,%s}", function, String.join(",", parameters));
System.out.println(combine);
}
}
Results:
{"setSpellName","Fireball"}
{"setSplashDamage","32","5"}
This is a solution using regex, use this Regex "([\\w]+)\\(\"?([\\w]+)\"?\\)":
String input = "setSpellName(\"Fireball\")";
String pattern = "([\\w]+)\\(\"?([\\w]+)\"?\\)";
Pattern r = Pattern.compile(pattern);
String[] matches;
Matcher m = r.matcher(input);
if (m.find()) {
System.out.println("Found value: " + m.group(1));
System.out.println("Found value: " + m.group(2));
String[] params = m.group(2).split(",");
if (params.length > 1) {
matches = new String[params.length + 1];
matches[0] = m.group(1);
System.out.println(params.length);
for (int i = 0; i < params.length; i++) {
matches[i + 1] = params[i];
}
System.out.println(String.join(" :: ", matches));
} else {
matches = new String[2];
matches[0] = m.group(1);
matches[1] = m.group(2);
System.out.println(String.join(", ", matches));
}
}
([\\w]+) is the first group to get the function name.
\\(\"?([\\w]+)\"?\\) is the second group to get the parameters.
This is a Working DEMO.