How to Trim All Elements in a List

How can I trim all elements in a list?

// you can omit the final ToArray call if you're using .NET 4
var result = string.Join(",", tl.Split(',').Select(s => s.Trim()).ToArray());

If you only need the final result string, rather than the intermediate collection, then you could use a regular expression to tidy the string. You'll need to benchmark to determine whether or not the regex outperforms the split-trim-join technique:

var result = Regex.Replace(tl, @"(?<=^|,) +| +(?=,|$)", "");

What's the best way to trim() all elements in a List<String>?

In Java 8, you should use something like:

List<String> trimmedStrings = 
originalStrings.stream().map(String::trim).collect(Collectors.toList());

also it is possible to use unary String::trim operator for elements of the initial list (untrimmed String instances will be overwritten) by calling this method:

originalStrings.replaceAll(String::trim);

Simplest Way to Trim All String within a List<string[]> using LINQ?

LINQ is for querying, It shouldn't be used for modifying existing collection. You can use the following, but it will return a new collection.

List<string[]> newList = list.Select(outer => outer
.Select(innerItem => innerItem.Trim())
.ToArray())
.ToList();

You may add checking against Null for each element in the string array before calling Trim to avoid NRE. Something like:

.Select(innerItem => innerItem != null ? innerItem.Trim() : null)

How do I trim specific elements of a list?

You can use replace to remove the brackets:

 lst = ['[0.111,', '-0.222]', '1', '2', '3']

lst2 = [x.replace('[','').replace(']','') for x in lst]

print(lst2)

Output

['0.111,', '-0.222', '1', '2', '3']

You also be more specific:

lst2 = [x[1:] if x[0] == '[' else x[:-1] if x[-1] == ']' else x for x in lst]

Trim text after character for every item in list - R

You are close, but using lapply with gsub, R doesn't know which arguments are which. You just need to label the arguments explicitly.

x <- list(c("a-b","b-c","c-d"),c("a-b","e-f"))
lapply(x, gsub, pattern = "^.*-", replacement = "")
[[1]]
[1] "b" "c" "d"

[[2]]
[1] "b" "f"

reflection trim all strings in list with list c#

public static IEnumerable<T> Trim<T>(this IEnumerable<T> collection)
where T:class
{
foreach (var item in collection)
{
var properties = item.GetType().GetProperties();

// Loop over properts
foreach (var property in properties)
{
if (property.PropertyType == typeof (string))
{
var currentValue = (string)property.GetValue(item);
if (currentValue != null)
property.SetValue(item, currentValue.Trim());
}
else if (typeof(IEnumerable<object>).IsAssignableFrom(property.PropertyType))
{
var currentValue = (IEnumerable<object>)property.GetValue(item);
if (currentValue != null)
currentValue.Trim();
}
}
}
return collection;
}

Edit: Included yield

Edit2: Removed yield again. I know this is bad practice for IEnumerable extensions. However the alternative would be:

            else if (typeof(IEnumerable<object>).IsAssignableFrom(property.PropertyType))
{
var currentValue = (IEnumerable<object>)property.GetValue(item);
if (currentValue != null)
currentValue.Trim().ToList(); // Hack to enumerate it!
}
}
}
yield return item;
}

How to trim array elements inside an array?

There are no issues with your code (the third version). The only misunderstanding is that .map() does not modify the original array, but return a new one. Look:

// This is basically your code.
const allData =
[
['Alex ','foo@email',' ',],
[' ','goo@gmail', '232-333-2222'],
];

const trimmedAllData = allData.map((data, index) => {
return data.map((content) => content.trim());
});

console.log(trimmedAllData);

How to Trim all what space from a list of data frames R

Use purrr and its map function to iterate over the list of data frames, then map_df to iterate over the columns in each data frame, which will return the results as data_frames.

library(purrr)
ParsedFile %>% map(~map_df(., ~trimws(.)))


Related Topics



Leave a reply



Submit