How to split a comma-separated string?
You could do this:
String str = "...";
List<String> elephantList = Arrays.asList(str.split(","));
Basically the .split()
method will split the string according to (in this case) delimiter you are passing and will return an array of strings.
However, you seem to be after a List of Strings rather than an array, so the array must be turned into a list by using the Arrays.asList()
utility. Just as an FYI you could also do something like so:
String str = "...";
ArrayList<String> elephantList = new ArrayList<>(Arrays.asList(str.split(","));
But it is usually better practice to program to an interface rather than to an actual concrete implementation, so I would recommend the 1st option.
Comma separated string split
Try:
a <- c("1,2,3","344")
scan(text = a, sep = ",", what = "")
# [1] "1" "2" "3" "344"
Split comma separated string and store into variables
Its as simple as :
var results = name.Split(',');
if(results.Length != 4)
throw new InvalidOperationException("Oh Noes!!!");
string fname = results[0];
string mname = results[1];
string lname = results[2];
string address= results[3];
Warning the second you have a comma in your address, this is going to fail.
If this is a CSV file, consider using a dedicated CSV Parser, there are plenty
Further reading
String.Split Method
Returns a string array that contains the substrings in this instance
that are delimited by elements of a specified string or Unicode
character array.
How to convert comma-separated String to List?
Convert comma separated String to List
List<String> items = Arrays.asList(str.split("\\s*,\\s*"));
The above code splits the string on a delimiter defined as: zero or more whitespace, a literal comma, zero or more whitespace
which will place the words into the list and collapse any whitespace between the words and commas.
Please note that this returns simply a wrapper on an array: you CANNOT for example .remove()
from the resulting List
. For an actual ArrayList
you must further use new ArrayList<String>
.
Split Comma separated Strings into Rows using Bigquery
Below is for BigQuery Standard SQL
#standardSQL
SELECT * EXCEPT(c) REPLACE(c AS country)
FROM `project.dataset.table`,
UNNEST(SPLIT(country)) c
If to apply to sample data from your question as in below example
#standardSQL
WITH `project.dataset.table` AS (
SELECT 'asia' region, 'india,china,japan' country, 100 revenue, 0.3 weight UNION ALL
SELECT 'europe', 'uk,france,germany,italy', 75, 0.25
)
SELECT * EXCEPT(c) REPLACE(c AS country)
FROM `project.dataset.table`,
UNNEST(SPLIT(country)) c
result is
Row region country revenue weight
1 asia india 100 0.3
2 asia china 100 0.3
3 asia japan 100 0.3
4 europe uk 75 0.25
5 europe france 75 0.25
6 europe germany 75 0.25
7 europe italy 75 0.25
How to split a comma-separated string into a list of strings?
You have to split the input using input().split(',')
.
a1=input().split(',')
print(a1)
Flutter/Dart - Split Comma Separated String into 3 Variables?
You could use a Map
, which is better suited for variables with dynamic length :
final tagName = 'grubs, sheep';
final split = tagName.split(',');
final Map<int, String> values = {
for (int i = 0; i < split.length; i++)
i: split[i]
};
print(values); // {0: grubs, 1: sheep}
final value1 = values[0];
final value2 = values[1];
final value3 = values[2];
print(value1); // grubs
print(value2); // sheep
print(value3); // null
How to Split Comma Separated String and Store it in to Variables VB Net
You can split your string and assign into variables like this:
Private Sub AssignVars()
Dim str As String = "1,5"
Dim num1, num2 As Integer
Dim results() As String
results = str.Split(Convert.ToChar(","))
For pos As Integer = 0 To results.Count - 1
Select Case pos
Case 0
Integer.TryParse(results(pos), num1)
Case 1
Integer.TryParse(results(pos), num2)
Case Else
'error handle?
End Select
Next
End Sub
If you're concerned about the for-case anti-pattern
Private Sub AssignVars()
Dim str As String = "1,5"
Dim num1, num2 As Integer
Dim results() As String
results = str.Split(Convert.ToChar(","))
Integer.TryParse(results(0), num1)
Integer.TryParse(results(1), num2)
End Sub
Related Topics
Only Using @JSONignore During Serialization, But Not Deserialization
Why Are Interface Variables Static and Final by Default
Mockito - Difference Between Doreturn() and When()
Java Ee 6 @Javax.Annotation.Managedbean VS. @Javax.Inject.Named VS. @Javax.Faces.Managedbean
How to Make a Java Thread Wait for Another Thread's Output
Add Jar Files to a Spark Job - Spark-Submit
What Is the "Owning Side" in an Orm Mapping
Java Time-Based Map/Cache with Expiring Keys
Arithmeticexception: "Non-Terminating Decimal Expansion; No Exact Representable Decimal Result"
How to Use a Delimiter with Scanner.Usedelimiter in Java
Explicitly Calling a Default Method in Java
How to Handle Calendar Timezones Using Java
Unresponsive Keylistener for Jframe
Synchronizing on String Objects in Java
How to Determine the Class of a Generic Type