Get everything in string after a single comma [duplicate]
This question already has an answer here:
How do I split a string with multiple separators in javascript?
16 answers
Convert comma separated string to array
11 answers
I have some problem with my string, the variable name is accountcode
. I want only part of the string. I want everything in the string which is after the first ,
, excluding any extra space after the comma. For example:
accountcode = "xxxx, tes";
accountcode = "xxxx, hello";
Then I want to output like tes
and hello
.
I tried:
var s = 'xxxx, hello';
s = s.substring(0, s.indexOf(','));
document.write(s);
javascript jquery string
marked as duplicate by Temani Afif, Makyen, Community♦ Nov 16 '18 at 1:59
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 4 more comments
This question already has an answer here:
How do I split a string with multiple separators in javascript?
16 answers
Convert comma separated string to array
11 answers
I have some problem with my string, the variable name is accountcode
. I want only part of the string. I want everything in the string which is after the first ,
, excluding any extra space after the comma. For example:
accountcode = "xxxx, tes";
accountcode = "xxxx, hello";
Then I want to output like tes
and hello
.
I tried:
var s = 'xxxx, hello';
s = s.substring(0, s.indexOf(','));
document.write(s);
javascript jquery string
marked as duplicate by Temani Afif, Makyen, Community♦ Nov 16 '18 at 1:59
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
So, just to clarify,accountcode
is an string with multiple words separated by commas?
– Shidersz
Nov 15 '18 at 4:11
1
@Mukyuu This is a javascript thread
– Abana Clara
Nov 15 '18 at 4:17
1
Why is this marked as a duplicate? This is not Java.
– Spencer Wieczorek
Nov 15 '18 at 4:19
1
I'm all for marking duplicates. But this one misses by a 6'2" ballerina split
– Abana Clara
Nov 15 '18 at 4:22
2
Weird duplicate vote... Either pick this or this as dupe.
– KarelG
Nov 15 '18 at 7:54
|
show 4 more comments
This question already has an answer here:
How do I split a string with multiple separators in javascript?
16 answers
Convert comma separated string to array
11 answers
I have some problem with my string, the variable name is accountcode
. I want only part of the string. I want everything in the string which is after the first ,
, excluding any extra space after the comma. For example:
accountcode = "xxxx, tes";
accountcode = "xxxx, hello";
Then I want to output like tes
and hello
.
I tried:
var s = 'xxxx, hello';
s = s.substring(0, s.indexOf(','));
document.write(s);
javascript jquery string
This question already has an answer here:
How do I split a string with multiple separators in javascript?
16 answers
Convert comma separated string to array
11 answers
I have some problem with my string, the variable name is accountcode
. I want only part of the string. I want everything in the string which is after the first ,
, excluding any extra space after the comma. For example:
accountcode = "xxxx, tes";
accountcode = "xxxx, hello";
Then I want to output like tes
and hello
.
I tried:
var s = 'xxxx, hello';
s = s.substring(0, s.indexOf(','));
document.write(s);
This question already has an answer here:
How do I split a string with multiple separators in javascript?
16 answers
Convert comma separated string to array
11 answers
javascript jquery string
javascript jquery string
edited Nov 15 '18 at 15:48
Makyen
20.7k84074
20.7k84074
asked Nov 15 '18 at 4:08
senaasenaa
3516
3516
marked as duplicate by Temani Afif, Makyen, Community♦ Nov 16 '18 at 1:59
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Temani Afif, Makyen, Community♦ Nov 16 '18 at 1:59
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
So, just to clarify,accountcode
is an string with multiple words separated by commas?
– Shidersz
Nov 15 '18 at 4:11
1
@Mukyuu This is a javascript thread
– Abana Clara
Nov 15 '18 at 4:17
1
Why is this marked as a duplicate? This is not Java.
– Spencer Wieczorek
Nov 15 '18 at 4:19
1
I'm all for marking duplicates. But this one misses by a 6'2" ballerina split
– Abana Clara
Nov 15 '18 at 4:22
2
Weird duplicate vote... Either pick this or this as dupe.
– KarelG
Nov 15 '18 at 7:54
|
show 4 more comments
So, just to clarify,accountcode
is an string with multiple words separated by commas?
– Shidersz
Nov 15 '18 at 4:11
1
@Mukyuu This is a javascript thread
– Abana Clara
Nov 15 '18 at 4:17
1
Why is this marked as a duplicate? This is not Java.
– Spencer Wieczorek
Nov 15 '18 at 4:19
1
I'm all for marking duplicates. But this one misses by a 6'2" ballerina split
– Abana Clara
Nov 15 '18 at 4:22
2
Weird duplicate vote... Either pick this or this as dupe.
– KarelG
Nov 15 '18 at 7:54
So, just to clarify,
accountcode
is an string with multiple words separated by commas?– Shidersz
Nov 15 '18 at 4:11
So, just to clarify,
accountcode
is an string with multiple words separated by commas?– Shidersz
Nov 15 '18 at 4:11
1
1
@Mukyuu This is a javascript thread
– Abana Clara
Nov 15 '18 at 4:17
@Mukyuu This is a javascript thread
– Abana Clara
Nov 15 '18 at 4:17
1
1
Why is this marked as a duplicate? This is not Java.
– Spencer Wieczorek
Nov 15 '18 at 4:19
Why is this marked as a duplicate? This is not Java.
– Spencer Wieczorek
Nov 15 '18 at 4:19
1
1
I'm all for marking duplicates. But this one misses by a 6'2" ballerina split
– Abana Clara
Nov 15 '18 at 4:22
I'm all for marking duplicates. But this one misses by a 6'2" ballerina split
– Abana Clara
Nov 15 '18 at 4:22
2
2
Weird duplicate vote... Either pick this or this as dupe.
– KarelG
Nov 15 '18 at 7:54
Weird duplicate vote... Either pick this or this as dupe.
– KarelG
Nov 15 '18 at 7:54
|
show 4 more comments
5 Answers
5
active
oldest
votes
Just use split
with trim
.
var accountcode = "xxxx, tes";
var result= accountcode.split(',')[1].trim();
console.log(result);
3
Why include jquery?
– dotconnor
Nov 15 '18 at 4:12
1
This will error when there is no comma. And may not work as expected if there are many commas.
– Spencer Wieczorek
Nov 15 '18 at 4:14
waaa thank you very much
– senaa
Nov 15 '18 at 4:14
add a comment |
You can use String.prototype.split()
:
The
split()
method splits a String object into an array of strings by separating the string into substrings, using a specified separator string to determine where to make each split.
You can use length property of the generated array as the last index to access the string item. Finally trim()
the string:
var s = 'xxxx, hello';
s = s.split(',');
s = s[s.length - 1].trim();
document.write(s);
add a comment |
You can use string.lastIndexOf()
to pull the last word out without making a new array:
let accountcode = "xxxx, hello";
let lastCommaIndex = accountcode.lastIndexOf(',')
let word = accountcode.slice(lastCommaIndex+1).trim()
console.log(word)
add a comment |
You can split
the String on the comma.
var s = 'xxxx, hello';
var parts = s.split(',');
console.log(parts[1]);
If you don't want any leading or trailing spaces, use trim
.
var s = 'xxxx, hello';
var parts = s.split(',');
console.log(parts[1].trim());
add a comment |
accountcode = "xxxx, hello";
let macthed=accountcode.match(/w+$/)
if(matched)
document.write(matched[0])
here w+
means any one or more charecter
and $
meand end of string
so w+$
means get all the character upto end of the sting
so here ' '
space is not a whole character so it started after space upto $
the if
statement is required because if no match found than macthed
will be null , and it found it will be an array and first element will be your match
add a comment |
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
Just use split
with trim
.
var accountcode = "xxxx, tes";
var result= accountcode.split(',')[1].trim();
console.log(result);
3
Why include jquery?
– dotconnor
Nov 15 '18 at 4:12
1
This will error when there is no comma. And may not work as expected if there are many commas.
– Spencer Wieczorek
Nov 15 '18 at 4:14
waaa thank you very much
– senaa
Nov 15 '18 at 4:14
add a comment |
Just use split
with trim
.
var accountcode = "xxxx, tes";
var result= accountcode.split(',')[1].trim();
console.log(result);
3
Why include jquery?
– dotconnor
Nov 15 '18 at 4:12
1
This will error when there is no comma. And may not work as expected if there are many commas.
– Spencer Wieczorek
Nov 15 '18 at 4:14
waaa thank you very much
– senaa
Nov 15 '18 at 4:14
add a comment |
Just use split
with trim
.
var accountcode = "xxxx, tes";
var result= accountcode.split(',')[1].trim();
console.log(result);
Just use split
with trim
.
var accountcode = "xxxx, tes";
var result= accountcode.split(',')[1].trim();
console.log(result);
var accountcode = "xxxx, tes";
var result= accountcode.split(',')[1].trim();
console.log(result);
var accountcode = "xxxx, tes";
var result= accountcode.split(',')[1].trim();
console.log(result);
edited Nov 15 '18 at 5:13
Abana Clara
1,646919
1,646919
answered Nov 15 '18 at 4:11
ShreeShree
12.8k2071123
12.8k2071123
3
Why include jquery?
– dotconnor
Nov 15 '18 at 4:12
1
This will error when there is no comma. And may not work as expected if there are many commas.
– Spencer Wieczorek
Nov 15 '18 at 4:14
waaa thank you very much
– senaa
Nov 15 '18 at 4:14
add a comment |
3
Why include jquery?
– dotconnor
Nov 15 '18 at 4:12
1
This will error when there is no comma. And may not work as expected if there are many commas.
– Spencer Wieczorek
Nov 15 '18 at 4:14
waaa thank you very much
– senaa
Nov 15 '18 at 4:14
3
3
Why include jquery?
– dotconnor
Nov 15 '18 at 4:12
Why include jquery?
– dotconnor
Nov 15 '18 at 4:12
1
1
This will error when there is no comma. And may not work as expected if there are many commas.
– Spencer Wieczorek
Nov 15 '18 at 4:14
This will error when there is no comma. And may not work as expected if there are many commas.
– Spencer Wieczorek
Nov 15 '18 at 4:14
waaa thank you very much
– senaa
Nov 15 '18 at 4:14
waaa thank you very much
– senaa
Nov 15 '18 at 4:14
add a comment |
You can use String.prototype.split()
:
The
split()
method splits a String object into an array of strings by separating the string into substrings, using a specified separator string to determine where to make each split.
You can use length property of the generated array as the last index to access the string item. Finally trim()
the string:
var s = 'xxxx, hello';
s = s.split(',');
s = s[s.length - 1].trim();
document.write(s);
add a comment |
You can use String.prototype.split()
:
The
split()
method splits a String object into an array of strings by separating the string into substrings, using a specified separator string to determine where to make each split.
You can use length property of the generated array as the last index to access the string item. Finally trim()
the string:
var s = 'xxxx, hello';
s = s.split(',');
s = s[s.length - 1].trim();
document.write(s);
add a comment |
You can use String.prototype.split()
:
The
split()
method splits a String object into an array of strings by separating the string into substrings, using a specified separator string to determine where to make each split.
You can use length property of the generated array as the last index to access the string item. Finally trim()
the string:
var s = 'xxxx, hello';
s = s.split(',');
s = s[s.length - 1].trim();
document.write(s);
You can use String.prototype.split()
:
The
split()
method splits a String object into an array of strings by separating the string into substrings, using a specified separator string to determine where to make each split.
You can use length property of the generated array as the last index to access the string item. Finally trim()
the string:
var s = 'xxxx, hello';
s = s.split(',');
s = s[s.length - 1].trim();
document.write(s);
var s = 'xxxx, hello';
s = s.split(',');
s = s[s.length - 1].trim();
document.write(s);
var s = 'xxxx, hello';
s = s.split(',');
s = s[s.length - 1].trim();
document.write(s);
edited Nov 15 '18 at 8:06
answered Nov 15 '18 at 4:14
MamunMamun
28.5k71831
28.5k71831
add a comment |
add a comment |
You can use string.lastIndexOf()
to pull the last word out without making a new array:
let accountcode = "xxxx, hello";
let lastCommaIndex = accountcode.lastIndexOf(',')
let word = accountcode.slice(lastCommaIndex+1).trim()
console.log(word)
add a comment |
You can use string.lastIndexOf()
to pull the last word out without making a new array:
let accountcode = "xxxx, hello";
let lastCommaIndex = accountcode.lastIndexOf(',')
let word = accountcode.slice(lastCommaIndex+1).trim()
console.log(word)
add a comment |
You can use string.lastIndexOf()
to pull the last word out without making a new array:
let accountcode = "xxxx, hello";
let lastCommaIndex = accountcode.lastIndexOf(',')
let word = accountcode.slice(lastCommaIndex+1).trim()
console.log(word)
You can use string.lastIndexOf()
to pull the last word out without making a new array:
let accountcode = "xxxx, hello";
let lastCommaIndex = accountcode.lastIndexOf(',')
let word = accountcode.slice(lastCommaIndex+1).trim()
console.log(word)
let accountcode = "xxxx, hello";
let lastCommaIndex = accountcode.lastIndexOf(',')
let word = accountcode.slice(lastCommaIndex+1).trim()
console.log(word)
let accountcode = "xxxx, hello";
let lastCommaIndex = accountcode.lastIndexOf(',')
let word = accountcode.slice(lastCommaIndex+1).trim()
console.log(word)
edited Nov 19 '18 at 2:03
hev1
5,8733529
5,8733529
answered Nov 15 '18 at 4:14
Mark MeyerMark Meyer
38.9k33160
38.9k33160
add a comment |
add a comment |
You can split
the String on the comma.
var s = 'xxxx, hello';
var parts = s.split(',');
console.log(parts[1]);
If you don't want any leading or trailing spaces, use trim
.
var s = 'xxxx, hello';
var parts = s.split(',');
console.log(parts[1].trim());
add a comment |
You can split
the String on the comma.
var s = 'xxxx, hello';
var parts = s.split(',');
console.log(parts[1]);
If you don't want any leading or trailing spaces, use trim
.
var s = 'xxxx, hello';
var parts = s.split(',');
console.log(parts[1].trim());
add a comment |
You can split
the String on the comma.
var s = 'xxxx, hello';
var parts = s.split(',');
console.log(parts[1]);
If you don't want any leading or trailing spaces, use trim
.
var s = 'xxxx, hello';
var parts = s.split(',');
console.log(parts[1].trim());
You can split
the String on the comma.
var s = 'xxxx, hello';
var parts = s.split(',');
console.log(parts[1]);
If you don't want any leading or trailing spaces, use trim
.
var s = 'xxxx, hello';
var parts = s.split(',');
console.log(parts[1].trim());
var s = 'xxxx, hello';
var parts = s.split(',');
console.log(parts[1]);
var s = 'xxxx, hello';
var parts = s.split(',');
console.log(parts[1]);
var s = 'xxxx, hello';
var parts = s.split(',');
console.log(parts[1].trim());
var s = 'xxxx, hello';
var parts = s.split(',');
console.log(parts[1].trim());
edited Nov 15 '18 at 4:19
answered Nov 15 '18 at 4:12
hev1hev1
5,8733529
5,8733529
add a comment |
add a comment |
accountcode = "xxxx, hello";
let macthed=accountcode.match(/w+$/)
if(matched)
document.write(matched[0])
here w+
means any one or more charecter
and $
meand end of string
so w+$
means get all the character upto end of the sting
so here ' '
space is not a whole character so it started after space upto $
the if
statement is required because if no match found than macthed
will be null , and it found it will be an array and first element will be your match
add a comment |
accountcode = "xxxx, hello";
let macthed=accountcode.match(/w+$/)
if(matched)
document.write(matched[0])
here w+
means any one or more charecter
and $
meand end of string
so w+$
means get all the character upto end of the sting
so here ' '
space is not a whole character so it started after space upto $
the if
statement is required because if no match found than macthed
will be null , and it found it will be an array and first element will be your match
add a comment |
accountcode = "xxxx, hello";
let macthed=accountcode.match(/w+$/)
if(matched)
document.write(matched[0])
here w+
means any one or more charecter
and $
meand end of string
so w+$
means get all the character upto end of the sting
so here ' '
space is not a whole character so it started after space upto $
the if
statement is required because if no match found than macthed
will be null , and it found it will be an array and first element will be your match
accountcode = "xxxx, hello";
let macthed=accountcode.match(/w+$/)
if(matched)
document.write(matched[0])
here w+
means any one or more charecter
and $
meand end of string
so w+$
means get all the character upto end of the sting
so here ' '
space is not a whole character so it started after space upto $
the if
statement is required because if no match found than macthed
will be null , and it found it will be an array and first element will be your match
answered Nov 15 '18 at 4:19
Pranoy SarkarPranoy Sarkar
583312
583312
add a comment |
add a comment |
So, just to clarify,
accountcode
is an string with multiple words separated by commas?– Shidersz
Nov 15 '18 at 4:11
1
@Mukyuu This is a javascript thread
– Abana Clara
Nov 15 '18 at 4:17
1
Why is this marked as a duplicate? This is not Java.
– Spencer Wieczorek
Nov 15 '18 at 4:19
1
I'm all for marking duplicates. But this one misses by a 6'2" ballerina split
– Abana Clara
Nov 15 '18 at 4:22
2
Weird duplicate vote... Either pick this or this as dupe.
– KarelG
Nov 15 '18 at 7:54