Home > Software engineering >  split string between last occurrence of of a to z of character in c #
split string between last occurrence of of a to z of character in c #

Time:01-27

I have one string for example "2244cc101" and I want to split this string into "2244cc" and "101" So I just need to split the string where last any character between a to z or A to Z without for loop take another example where s224s01 that should split like "s224s" and "01".

CodePudding user response:

it may be complicated, but try this

let string = "2244cc101"
let string2 = "s224s01"

function splitStr(string) {
  let arr = string.split('');
  for (let j = arr.length; j--;) {
    if (arr[j].match(/[a-zA-Z]/)) {
      let last = arr[j]
      return [string.slice(0, string.lastIndexOf(last)   1), string.slice(string.lastIndexOf(last)   1)];
    }
  }
}

console.log(splitStr(string))
console.log(splitStr(string2))

CodePudding user response:

Use a Regular Expression to determine the position of the first occourance of a non-letter in the string, then split the string on that position.

  •  Tags:  
  • Related