logo
down
shadow

Can `strings.split` ignore empty tokens?


Can `strings.split` ignore empty tokens?

By : Quickfixer
Date : October 18 2020, 03:08 PM
wish of those help Short answer: strings.Split can't do that.
However, there are more functions to split strings in Go. Notably, you can do what you want with strings.FieldsFunc. The example here:
code :
splitFn := func(c rune) bool {
        return c == ','
}
fmt.Printf("Fields are: %q\n", strings.FieldsFunc("a,,b,c", splitFn))


Share : facebook icon twitter icon
Fastest way to split a concatenated string into a tuple and ignore empty strings

Fastest way to split a concatenated string into a tuple and ignore empty strings


By : praveen kumar
Date : March 29 2020, 07:55 AM
may help you . That is a very reasonable way to do it. Some alternatives:
foo.strip(";").split(";") (if there won't be any empty slices inside the string) [ x.strip() for x in foo.split(";") if x.strip() ] (to strip whitespace from each slice)
code :
In [1]: foo = "1;2;3;4;"

In [2]: %timeit foo.strip(";").split(";")
1000000 loops, best of 3: 1.03 us per loop

In [3]: %timeit filter(None, foo.split(';'))
1000000 loops, best of 3: 1.55 us per loop
How to use boost split to split a string and ignore empty values?

How to use boost split to split a string and ignore empty values?


By : Roi Vito Peleg
Date : March 29 2020, 07:55 AM
wish helps you Even though "adjacent separators are merged together", it seems like the trailing delimeters make the problem, since even when they are treated as one, it still is one delimeter.
So your problem cannot be solved with split() alone. But luckily Boost String Algo has trim() and trim_if(), which strip whitespace or delimeters from beginning and end of a string. So just call trim() on buf, like this:
code :
std::string buf = "1:1~15  ASTKGPSVFPLAPSS SVFPLAPSS   -12.6   98.3    ";
std::vector<std::string> dataLine;
boost::trim_if(buf, boost::is_any_of("\t ")); // could also use plain boost::trim
boost::split(dataLine, buf, boost::is_any_of("\t "), boost::token_compress_on);
std::cout << out.size() << std::endl;
Batch file tokens ignore empty delimiters

Batch file tokens ignore empty delimiters


By : Vincent Cardoen
Date : March 29 2020, 07:55 AM
it should still fix some issue for treats consecutive delimiters as one. In most cases, this is helpful. Sometimes it is not.
So you have to write your lines in a way, that for can handle as you intended.
code :
"chris","","steve","deon","bryan","mark","anthony"
@echo off
setlocal enabledelayedexpansion
FOR /F "tokens=*" %%a in (csvfile.csv) do (
  set line="%%a"
  for /f "tokens=3 delims=," %%b in ("!line:,="^,"!") do echo %%~b
)
REGEX: split javascript strings - ignore commas in nested strings and object-likes

REGEX: split javascript strings - ignore commas in nested strings and object-likes


By : kalai91
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , You can use this regex to split on commas not under curley braces by using negative look ahead to ensure , is not contained in curley braces.
code :
,(?![^{}]*\})
var str = ['2,5,4','2,{"name": [2,3]}',' 2,{"key": "value", "key2":"value2" } '];
for (i=0;i<str.length;i++) {
    console.log(str[i] + " --> split into following");
    str[i] = str[i].split(/,(?![^{}]*\})/);
    for (j = 0; j < str[i].length; j++) { 
        console.log(str[i][j]);
    }
    console.log('\n\n');    
}
Reading int, double and strings and ignore tokens

Reading int, double and strings and ignore tokens


By : Sutapa Bhattacharya
Date : March 29 2020, 07:55 AM
I wish this helpful for you , This is rather ugly but it will work,
shadow
Privacy Policy - Terms - Contact Us © soohba.com