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);
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));
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!
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
Splitting a string at a particular position in java
I would use the String.substring(beginIndex, endIndex); and String.substring(beginIndex);
String a = "word1 word2 word3 word4";
int first = a.indexOf(" ");
int second = a.indexOf(" ", first + 1);
String b = a.substring(0,second);
String c = b.subString(second); // Only startindex, cuts at the end of the string
This would result in a = "word1 word2" and b = "word3 word4"
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);
How do I split a string in Java?
Use the appropriately named method String#split()
.
String string = "004-034556";
String[] parts = string.split("-");
String part1 = parts[0]; // 004
String part2 = parts[1]; // 034556
Note that split
's argument is assumed to be a regular expression, so remember to escape special characters if necessary.
there are 12 characters with special meanings: the backslash
\
, the caret^
, the dollar sign$
, the period or dot.
, the vertical bar or pipe symbol|
, the question mark?
, the asterisk or star*
, the plus sign+
, the opening parenthesis(
, the closing parenthesis)
, and the opening square bracket[
, the opening curly brace{
, These special characters are often called "metacharacters".
For instance, to split on a period/dot .
(which means "any character" in regex), use either backslash \
to escape the individual special character like so split("\\.")
, or use character class []
to represent literal character(s) like so split("[.]")
, or use Pattern#quote()
to escape the entire string like so split(Pattern.quote("."))
.
String[] parts = string.split(Pattern.quote(".")); // Split on the exact string.
To test beforehand if the string contains certain character(s), just use String#contains()
.
if (string.contains("-")) {
// Split it.
} else {
throw new IllegalArgumentException("String " + string + " does not contain -");
}
Note, this does not take a regular expression. For that, use String#matches()
instead.
If you'd like to retain the split character in the resulting parts, then make use of positive lookaround. In case you want to have the split character to end up in left hand side, use positive lookbehind by prefixing ?<=
group on the pattern.
String string = "004-034556";
String[] parts = string.split("(?<=-)");
String part1 = parts[0]; // 004-
String part2 = parts[1]; // 034556
In case you want to have the split character to end up in right hand side, use positive lookahead by prefixing ?=
group on the pattern.
String string = "004-034556";
String[] parts = string.split("(?=-)");
String part1 = parts[0]; // 004
String part2 = parts[1]; // -034556
If you'd like to limit the number of resulting parts, then you can supply the desired number as 2nd argument of split()
method.
String string = "004-034556-42";
String[] parts = string.split("-", 2);
String part1 = parts[0]; // 004
String part2 = parts[1]; // 034556-42
Related Topics
How to Update Timezonedb in PHP (Updating Timezones Info)
Setting Variables on Constructor VS on the Class Definition
How to Disable or Encrypt "View Source" for My Site
Generating a Random Code in PHP
How to Convert an Integer to an Array in PHP
PHP $_Server['Remote_Addr'] Shows Ipv6
How to Recursively Obtain the "Parent Id" of Rows in This MySQL Table
PHP Fatal Error: Call to Undefined Function Mssql_Query()
How to Apply Url Normalization Rules in PHP
Reply-To Address in PHP Contact Form
Add Prefix to Auto-Increment in MySQL Db
How to Convert a PHP Query String into a Slash-Based Url
How to Make First Letter of a Word Capital