Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cut out part of a string

Tags:

Say, I have a string

"hello is it me you're looking for" 

I want to cut part of this string out and return the new string, something like

s = string.cut(0,3); 

s would now be equal to:

"lo is it me you're looking for" 

EDIT: It may not be from 0 to 3. It could be from 5 to 7.

s = string.cut(5,7); 

would return

"hellos it me you're looking for" 
like image 878
dotty Avatar asked Nov 10 '09 12:11

dotty


People also ask

How do I cut out part of a string?

The slice() method returns the extracted part in a new string. The slice() method does not change the original string. The start and end parameters specifies the part of the string to extract.

How do you cut a section of a string in Java?

The trim() method in Java String is a built-in function that eliminates leading and trailing spaces. The Unicode value of space character is '\u0020'.

How do you cut the last word of a string?

To remove the last word from a string, get the index of the last space in the string, using the lastIndexOf() method. Then use the substring() method to get a portion of the string with the last word removed.


1 Answers

You're almost there. What you want is:

http://www.w3schools.com/jsref/jsref_substr.asp

So, in your example:

Var string = "hello is it me you're looking for"; s = string.substr(3); 

As only providing a start (the first arg) takes from that index to the end of the string.

Update, how about something like:

function cut(str, cutStart, cutEnd){   return str.substr(0,cutStart) + str.substr(cutEnd+1); } 
like image 124
Jake Avatar answered Sep 22 '22 16:09

Jake