I have this arrayList that receives data dynamically from a database
val deviceNameList = arrayListOf<String>()
Getting the index 0 of the arraylist ie deviceNameList[0] prints a string of such a format:
[Peter, James]
How can i list all names in deviceNameList[0] individually.
Assuming your input string is [Peter, James], you could try removing the square brackets at both ends, then regex splitting on comma followed by optional whitespace.
String input = "[Peter, James]";
String[] names = input.substring(1, input.length()-1).split(",\\s*");
System.out.println(Arrays.toString(names));
This prints:
[Peter, James]
Note that Java itself places square brackets around the array contents in Arrays.toString. They are not part of the actual data.
Related
I have converted list to string.
but after conversion I am getting string without single quote around the string
for eg:
items = ['aa','bb','cc']
items = ','.join(items)
output is : aa,bb,cc
expected output: 'aa','bb','cc'
You could use a list comprehension to quote the individual strings in the list:
items = ['aa','bb','cc']
items = ','.join([f"'{i}'" for i in items])
print(items) # 'aa','bb','cc'
One way to accomplish this is by passing the list into a string formatter, which will place the outer quotes around each list element. The list is mapped to the formatter, then joined, as you have shown.
For example:
','.join(map("'{}'".format, items))
Output:
"'aa','bb','cc'"
I had to split string data based on Comma.
This is the excel data:-
Please find the excel data
string strCurrentLine="\"Himalayan Salt Body Scrub with Lychee Essential Oil from Majestic Pure, All Natural Scrub to Exfoliate & Moisturize Skin, 12 oz\",SKU_27,\"Tombow Dual Brush Pen Art Markers, Portrait, 6-Pack\",SKU_27,My Shopify Store 1,Valid,NonInventory".
Regex CSVParser = new Regex(",(?=(?:[^\"]\"[^\"]\")(?![^\"]\"))");
string[] lstColumnValues = CSVParser.Split(strCurrentLine);
I have attached the image.The problem is I used the Regex to split the string with comma but i need the ouptut just like SKU_27 because string[0] and string2 contains the forward and backward slash.I need the output string1 and remove the forward and backward slash.
The file seems to be a CVA file. For CVA to be properly formatted, it will use quotes "" to wrap strings that contains comma, such as
id, name, date
1,"Some text, that includes comma", 2020/01/01
Simply split the string by comma, you will get the 2nd column with double quote.
I'm not sure whether you are asking how to remove the double-quotes from lstColumnValues[0] and lstColumnValues[2], or add them to lstColumnValues[1].
To remove the double-quotes, just use Replace:
string myString = lstColumnValues[0].Replace("\"", "");
If you need to add them:
string myString = $"\"{lstColumnValues[1]}\"";
I'm new to Scala and unsure of how to achieve the following
I have the String
val output = "6055039\n3000457596\n3000456748\n180013\n"
I want to extract the numbers separated by \n and store them in an Array
output.split("\n").map(_.toInt)
Or only
output.split("\n")
if you want to keep the numbers in String format. Note that .toInt throws. You might want to wrap it accordingly.
Trying to use rstrip() at its most basic level, but it does not seem to have any effect at all.
For example:
string1='text&moretext'
string2=string1.rstrip('&')
print(string2)
Desired Result:
text
Actual Result:
text&moretext
Using Python 3, PyScripter
What am I missing?
someString.rstrip(c) removes all occurences of c at the end of the string. Thus, for example
'text&&&&'.rstrip('&') = 'text'
Perhaps you want
'&'.join(string1.split('&')[:-1])
This splits the string on the delimiter "&" into a list of strings, removes the last one, and joins them again, using the delimiter "&". Thus, for example
'&'.join('Hello&World'.split('&')[:-1]) = 'Hello'
'&'.join('Hello&Python&World'.split('&')[:-1]) = 'Hello&Python'
I have a file with chinese content that I need to parse. Each post has some weird delimitter between fields and I am trying to isolate the fields but cannot recognize the delimitter.
Dim stringSplitter() as string = {" "}
Try
sampleResults = entry.Split(stringSplitter,StringSplitOptions.RemoveEmptyEntries)
.....
A sample of the post content;
108087006686338t.qq.com/GAOCHUANG8899homeGAOCHUANG8899homehttp://t.qq.com/p/t/1080870066863382012-03-22 04:49:46
The separator starts after the first set of digits 108087006686338 DELIMITTER t.qq.com/GAOCHUANG8899home . I initially thought I could split it using json but this is definitely not json format.
Sorry when I post the original the delimitters disappear when making this post. The delimitter looks like a rectangular block
EDIT:
Ok using the hex editor I identified the character hex value as 01 and it looks like a period but the period has a value of 2E. Does this mean anything to anyone?
EDIT:
Reproducing the question: can I split a string based on a hex value. If the value is "01" then how would I split the string based on that value.
EDIT:
final answer:`
Dim hvalue as Char = Char(1)
Dim stringSplitter() as string = {hvalue}
Let's say you have input $input and delimitter with ascii code of 01.
Perl:
my $input = ...
my #output = split(chr(01), $input);
print "$_\t" for #output; # print all items
The code above will split your $input into #output array, so then you can access items via
$output[0] # first item
$output[1] # second item
...
$#output + 1 # number of items
Visual-Studio-2010:
Dim hvalue as Char = Char(1)
Dim stringSplitter() as string = {hvalue}