How to check if arrays String element contains number

Let's say I have an array:

var myArray = ["bla_01", "bla_02", "tra", "bla", "tra_01", "tra_14]

I want to sort them and get only strings, that contain numbers in it. So my func would have to return only "bla_01", "bla_02", "tra_01", "tra_14" values.

I have a code like this now, but this will get only the first values and if there is item_01, item_02, bla_02 this will drop only item_01, item_02 and ignore bla_02. And later to sort and return only same name values with the highest number.

    func unequeValues() {
    var number = 1
    for item in tricks {
        if item.contains("\(number)") {
            number += 1



Filter the array with regular expression (skip items without digits) and sort it numerically

let myArray = ["bla_01", "bla_02", "tra", "bla", "tra_01", "tra_14"]
let filteredArray = myArray.filter{$0.range(of: "[0-9]+", options: .regularExpression) != nil }
                           .sorted{$0.localizedStandardCompare($1) == .orderedAscending}

or more efficient if the last good character is always a number

let myArray = ["bla_01", "bla_02", "tra", "bla", "tra_01", "tra_14"]
let filteredArray = myArray.filter{$0.last?.isNumber == true }
                           .sorted{$0.localizedStandardCompare($1) == .orderedAscending}

The rangeOfCharacter(from solution can be considerably improved by adding the backwards option

let filteredArray = myArray.filter{$0.rangeOfCharacter(from: .decimalDigits, options: .backwards) != nil }
                           .sorted{$0.localizedStandardCompare($1) == .orderedAscending} 

How to Check if an Array Contains a Value in Java Efficiently?, Four Different Ways to Check If an Array Contains a Value If the array is sorted, you can use the following code to search the target element: public static boolean useArraysBinarySearch(String[] arr, String targetValue) { int a = Arrays. Four Different Ways to Check If an Array Contains a Value. Using List:. public static boolean useList(String[] arr, String targetValue) { return Arrays.asList(arr).contains(targetValue); }

var myArray = ["bla_01", "bla_02", "tra", "bla", "tra_01", "tra_14"]

let newArray = myArray.filter { (substring) -> Bool in
    let numbersRange = substring.rangeOfCharacter(from: .decimalDigits)
    let hasNumbers = (numbersRange != nil)
    return hasNumbers

Java - Check if Array contains a certain value?, Java examples to check if an Array (String or Primitive type) contains a certain 2.1 For primitive array like int[] , you need to loop it and test the� The includes() method determines whether an array contains a specified element. This method returns true if the array contains the element, and false if not. Note: The includes() method is case sensitive.

Simple Swift 4 version using rangeOfCharacter method from String class:

let myArray = ["bla_01", "bla_02", "tra", "bla", "tra_01", "tra_14"]

let filteredArray = myArray.filter{$0.rangeOfCharacter(from: .decimalDigits) != nil }
                               .sorted{$0.localizedStandardCompare($1) == .orderedAscending}

Check if a value is present in an Array in Java, Given an array, the task is to check whether a certain element is present in this Array or not, in Java. for (int element : arr) { if (element == toCheckValue) { return true; } } public static void main(String[] args) Using List.contains() Method: List contains() method in Java is used for checking if the specified� In the above program, we have an array of integers stored in variable num. Likewise, the number to be found is stored in toFind. Now, we use a foreach loop to iterate through all elements of num and check individually if toFind is equal to n or not. If yes, we set found to true and break from the loop. If not, we move to the next iteration.

Java Program to Check if An Array Contains a Given Value, In this program, you'll learn to check if an array contains a given value in Java. public class Contains { public static void main(String[] args) { int[] num = {1, 2, 3, 4 , Now, we use a foreach loop to iterate through all elements of num and check � Check whether array has all identical elements using Arrays.asList() and HashSet in Java Java program to read all mobile numbers present in given file Check if the given string of words can be formed from words present in the dictionary

JavaScript Array includes() Method, This method returns true if the array contains the element, and false if not. Note: The Check if an array includes "Banana", starting the search at position 3:. ArrayList contains() method is used to check if the specified element exists in the given arraylist or not. If element exist then method returns true, else false. 1. ArrayList contains() syntax. The contains() method is pretty simple. It simply checks the index of element in the list. If the index is greater than '0' than element is present in

How to check if array includes a value in JavaScript?, Here's a Code Recipe to check if a #JavaScript array contains a value. Besides strings, includes also works great with other primitive types . ".some()": tests whether at least one element in the array passes the test implemented @ smokku : Bitwise not gives you the opposite of the number, but we use two's complement� To find whether a given string contains a number, convert it to a character array and find whether each character in the array is a digit using the isDigit() method of the Character class. Example Live Demo

  • Where is your function? Have you tried it for yourself?
  • Hey El Tomato. I have tried in various ways but don't know how to sort it and find not all value like : "bla" but the part of the value "01" , "02"... : func unequeValues() { for item in myArray { if item.contains("01") { print(item) } } }
  • And if I want to return only the highest values of the array, how to do that? As I used for item in myArray { item.suffix(2) } I could convert that last 2 numbers into a Int and then compare because the type is different.