I have a string
with a lot of characters. I would like to split the string
into 2 sub-strings. I don't need to use getfirsthalf()
and getsecondhalf()
, but that is the idea of what i need to achieve.
var pleet = "This is the string with a lot of characters";
var part1 = pleet.getFirstHalf();
var part2 = pleet.getSecondHalf()
//output
var part1 = "This is the string wi";
var part2 = "th a lot of characters";
I have a string
with a lot of characters. I would like to split the string
into 2 sub-strings. I don't need to use getfirsthalf()
and getsecondhalf()
, but that is the idea of what i need to achieve.
var pleet = "This is the string with a lot of characters";
var part1 = pleet.getFirstHalf();
var part2 = pleet.getSecondHalf()
//output
var part1 = "This is the string wi";
var part2 = "th a lot of characters";
Share
Improve this question
edited Sep 1, 2015 at 7:52
Tushar
87.3k21 gold badges163 silver badges181 bronze badges
asked Jul 31, 2015 at 11:03
user3432681user3432681
6644 gold badges12 silver badges27 bronze badges
3
- what effort have you made? – Daniel A. White Commented Jul 31, 2015 at 11:04
- 1 What defines 'half' of a string? – BenM Commented Jul 31, 2015 at 11:04
- @DontVoteMeDown What if the string has 7 characters? – BenM Commented Jul 31, 2015 at 11:10
4 Answers
Reset to default 4You can use substring() with the length
of the string to divide the string in two parts by using the index.
The substring() method returns a subset of a string between one index and another, or through the end of the string.
var pleet = "This is the string with a lot of characters";
var len = pleet.length;
var firstHalf = pleet.substring(0, len / 2);
var secondHalf = pleet.substring(len / 2);
document.write(firstHalf);
document.write('<br />');
document.write(secondHalf);
You can also use substr()
You must to be more specific in your questions. But here you are a simply solution:
var str = "an string so long with the characters you need";
var strLength = str.length;
console.log(str.substring(0 , (strLength / 2));
console.log(str.substring((strLength / 2));
Assuming that when you say 'half' a string, you actually mean that two separate strings are returned containing half of the original string's characters each, you could write a prototype function to handle that as follows:
String.prototype.splitInHalf = function()
{
var len = this.length,
first = Math.ceil( len / 2 );
return [
this.substring(0, first),
this.substring(first)
];
}
When called as pleet.splitInHalf()
, This function will return an array containing the two halves, as follows:
["This is the string wit", "h a lot of characters"]
Since we use Math.ceil()
here, the prototype will also favour the first half of the string. For example, given a string that has an odd number of characters, such as This is
, the returned array will contain 4 characters in the first string, and 3 in the second, as follows:
["This", " is"]
jsFiddle Demo
Here is class example:
class strSplitterHelper {
constructor(str) {
this.str = str;
this.strLength = str.length;
}
devide() {
const position = this.getNearestSpace();
if (position === null) {
return [this.str, null];
}
return [
this.str.substr(0, position),
this.str.substr(position + 1)
];
}
getNearestSpace() {
const middle = Math.floor(this.strLength / 2);
for (let i = middle; i >= 0; i--) {
if (this.str.charAt(i) === ' ') {
return i;
}
if (this.str.charAt(this.strLength - i) === ' ') {
return this.strLength - i;
}
}
return null;
}
}
const parts = new strSplitterHelper(
'This is the string with a lot of characters'
).devide();
that will return 2 elements array.