How to Split String into 2 Parts After Certain Position

How to split string into 2 parts after certain position

This should do it

[str[0..5], str[6..-1]]

or

 [str.slice(0..5), str.slice(6..-1)]

Really should check out http://corelib.rubyonrails.org/classes/String.html

split string in two on given index and return both parts

Try this

function split_at_index(value, index)
{
return value.substring(0, index) + "," + value.substring(index);
}

console.log(split_at_index('3123124', 2));

Split string into two parts only

You could use a,b = split(' ', 1).

The second argument 1 is the maximum number of splits that would be done.

s = 'abcd efgh hijk'
a,b = s.split(' ', 1)
print(a) #abcd
print(b) #efgh hijk

For more information on the string split function, see str.split in the manual.

how do i split this string into two starting from a specific position

There are many ways to achive this.

You could split your string on commas with split(), then get the last element of the created array with slice(). Then join() the elements left in the first part back into a string.

var str = '"123", "bankole","wale","","","" and  ""';var arr = str.split(',')var start = arr.slice(0, -1).join(',')var end = arr[arr.length-1];
console.log(start);console.log(end);

separate string in two by given position

You can use substr to get the two sub-strings:

$str1 = substr($str, 0, $pos);
$str2 = substr($str, $pos);

If you omit the third parameter length, substr takes the rest of the string.

But to get your result you actually need to add one to $pos:

$string = 'Some string';
$pos = 5;
$begin = substr($string, 0, $pos+1);
$end = substr($string, $pos+1);

Python: split a string by the position of a character

You can do this with strings (and lists) using slicing:

string = "hello world!"
splitat = 4
left, right = string[:splitat], string[splitat:]

will result in:

>>> left
hell
>>> right
o world!

Split string into two parts

Use

var someString = "A04.3  A new Code";
var index = someString.indexOf(" "); // Gets the first index where a space occours
var id = someString.substr(0, index); // Gets the first part
var text = someString.substr(index + 1); // Gets the text part

How to split a string into substrings of a given length?

Here is one way

substring("aabbccccdd", seq(1, 9, 2), seq(2, 10, 2))
#[1] "aa" "bb" "cc" "cc" "dd"

or more generally

text <- "aabbccccdd"
substring(text, seq(1, nchar(text)-1, 2), seq(2, nchar(text), 2))
#[1] "aa" "bb" "cc" "cc" "dd"

Edit: This is much, much faster

sst <- strsplit(text, "")[[1]]
out <- paste0(sst[c(TRUE, FALSE)], sst[c(FALSE, TRUE)])

It first splits the string into characters. Then, it pastes together the even elements and the odd elements.

Timings

text <- paste(rep(paste0(letters, letters), 1000), collapse="")
g1 <- function(text) {
substring(text, seq(1, nchar(text)-1, 2), seq(2, nchar(text), 2))
}
g2 <- function(text) {
sst <- strsplit(text, "")[[1]]
paste0(sst[c(TRUE, FALSE)], sst[c(FALSE, TRUE)])
}
identical(g1(text), g2(text))
#[1] TRUE
library(rbenchmark)
benchmark(g1=g1(text), g2=g2(text))
# test replications elapsed relative user.self sys.self user.child sys.child
#1 g1 100 95.451 79.87531 95.438 0 0 0
#2 g2 100 1.195 1.00000 1.196 0 0 0


Related Topics



Leave a reply



Submit