Split string with character :
up vote
-1
down vote
favorite
I have a string which is ant: man : jack
.
I want to split the string from :
so that I get man : jack
as the output.
Note: The string should split from the first occurrence of the :
character. how can I do this ?
What I tried works, But I need another method to produce this result.
var.Substring(var.IndexOf(':') + 1);
string split = var.Split(new char { ':' }, 2);
split[1] = split[1].TrimStart();
c#
add a comment |
up vote
-1
down vote
favorite
I have a string which is ant: man : jack
.
I want to split the string from :
so that I get man : jack
as the output.
Note: The string should split from the first occurrence of the :
character. how can I do this ?
What I tried works, But I need another method to produce this result.
var.Substring(var.IndexOf(':') + 1);
string split = var.Split(new char { ':' }, 2);
split[1] = split[1].TrimStart();
c#
5
Why do you need another method? What's wrong with your current method?
– John
Nov 20 at 1:47
2
Substring(var.IndexOf(':') + 1);
will be the fastest and least verbose, what is your actual problem why cant you use it
– TheGeneral
Nov 20 at 2:03
@TheGeneral the question was only live 30 minutes before your comment. Can we be fair and recognize that people may post and step away for a meeting, dinner, time with family, sleep?
– stealththeninja
Nov 20 at 2:41
@stealththeninja if you add the appropriate information ill be more than happy to remove the votes.
– TheGeneral
Nov 20 at 2:44
do you need both strings ("ant" and "man : jack") as a result?
– vasily.sib
Nov 20 at 3:27
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I have a string which is ant: man : jack
.
I want to split the string from :
so that I get man : jack
as the output.
Note: The string should split from the first occurrence of the :
character. how can I do this ?
What I tried works, But I need another method to produce this result.
var.Substring(var.IndexOf(':') + 1);
string split = var.Split(new char { ':' }, 2);
split[1] = split[1].TrimStart();
c#
I have a string which is ant: man : jack
.
I want to split the string from :
so that I get man : jack
as the output.
Note: The string should split from the first occurrence of the :
character. how can I do this ?
What I tried works, But I need another method to produce this result.
var.Substring(var.IndexOf(':') + 1);
string split = var.Split(new char { ':' }, 2);
split[1] = split[1].TrimStart();
c#
c#
asked Nov 20 at 1:45
Illep
6,17529130218
6,17529130218
5
Why do you need another method? What's wrong with your current method?
– John
Nov 20 at 1:47
2
Substring(var.IndexOf(':') + 1);
will be the fastest and least verbose, what is your actual problem why cant you use it
– TheGeneral
Nov 20 at 2:03
@TheGeneral the question was only live 30 minutes before your comment. Can we be fair and recognize that people may post and step away for a meeting, dinner, time with family, sleep?
– stealththeninja
Nov 20 at 2:41
@stealththeninja if you add the appropriate information ill be more than happy to remove the votes.
– TheGeneral
Nov 20 at 2:44
do you need both strings ("ant" and "man : jack") as a result?
– vasily.sib
Nov 20 at 3:27
add a comment |
5
Why do you need another method? What's wrong with your current method?
– John
Nov 20 at 1:47
2
Substring(var.IndexOf(':') + 1);
will be the fastest and least verbose, what is your actual problem why cant you use it
– TheGeneral
Nov 20 at 2:03
@TheGeneral the question was only live 30 minutes before your comment. Can we be fair and recognize that people may post and step away for a meeting, dinner, time with family, sleep?
– stealththeninja
Nov 20 at 2:41
@stealththeninja if you add the appropriate information ill be more than happy to remove the votes.
– TheGeneral
Nov 20 at 2:44
do you need both strings ("ant" and "man : jack") as a result?
– vasily.sib
Nov 20 at 3:27
5
5
Why do you need another method? What's wrong with your current method?
– John
Nov 20 at 1:47
Why do you need another method? What's wrong with your current method?
– John
Nov 20 at 1:47
2
2
Substring(var.IndexOf(':') + 1);
will be the fastest and least verbose, what is your actual problem why cant you use it– TheGeneral
Nov 20 at 2:03
Substring(var.IndexOf(':') + 1);
will be the fastest and least verbose, what is your actual problem why cant you use it– TheGeneral
Nov 20 at 2:03
@TheGeneral the question was only live 30 minutes before your comment. Can we be fair and recognize that people may post and step away for a meeting, dinner, time with family, sleep?
– stealththeninja
Nov 20 at 2:41
@TheGeneral the question was only live 30 minutes before your comment. Can we be fair and recognize that people may post and step away for a meeting, dinner, time with family, sleep?
– stealththeninja
Nov 20 at 2:41
@stealththeninja if you add the appropriate information ill be more than happy to remove the votes.
– TheGeneral
Nov 20 at 2:44
@stealththeninja if you add the appropriate information ill be more than happy to remove the votes.
– TheGeneral
Nov 20 at 2:44
do you need both strings ("ant" and "man : jack") as a result?
– vasily.sib
Nov 20 at 3:27
do you need both strings ("ant" and "man : jack") as a result?
– vasily.sib
Nov 20 at 3:27
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
We could try doing a regex replace:
string input = "ant: man : jack";
string s = Regex.Replace(input, @"^[^:]+s*:s*", "");
Console.WriteLine(s);
man : jack
But, I think that splitting actually would scale better given your input string.
add a comment |
up vote
0
down vote
You are interested in only the substring after colon, so you want to drop the characters before, you need to use skipWhile
:
string test = "ant:man:jack";
var results = test.SkipWhile(t => t != ':').Skip(0).ToList();
Remember skipWhile
will skip characters till the predicate is true, that means at first instance of : it will return a list of chars.
"It will return the substring" - but it doesn't? AList<char>
does not astring
maketh! Also theremoveAt(0)
could easily be replaced with an extra.Skip(1)
after your.SkipWhile()
.
– John
Nov 20 at 2:46
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385088%2fsplit-string-with-character%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
We could try doing a regex replace:
string input = "ant: man : jack";
string s = Regex.Replace(input, @"^[^:]+s*:s*", "");
Console.WriteLine(s);
man : jack
But, I think that splitting actually would scale better given your input string.
add a comment |
up vote
0
down vote
We could try doing a regex replace:
string input = "ant: man : jack";
string s = Regex.Replace(input, @"^[^:]+s*:s*", "");
Console.WriteLine(s);
man : jack
But, I think that splitting actually would scale better given your input string.
add a comment |
up vote
0
down vote
up vote
0
down vote
We could try doing a regex replace:
string input = "ant: man : jack";
string s = Regex.Replace(input, @"^[^:]+s*:s*", "");
Console.WriteLine(s);
man : jack
But, I think that splitting actually would scale better given your input string.
We could try doing a regex replace:
string input = "ant: man : jack";
string s = Regex.Replace(input, @"^[^:]+s*:s*", "");
Console.WriteLine(s);
man : jack
But, I think that splitting actually would scale better given your input string.
answered Nov 20 at 1:51
Tim Biegeleisen
214k1386134
214k1386134
add a comment |
add a comment |
up vote
0
down vote
You are interested in only the substring after colon, so you want to drop the characters before, you need to use skipWhile
:
string test = "ant:man:jack";
var results = test.SkipWhile(t => t != ':').Skip(0).ToList();
Remember skipWhile
will skip characters till the predicate is true, that means at first instance of : it will return a list of chars.
"It will return the substring" - but it doesn't? AList<char>
does not astring
maketh! Also theremoveAt(0)
could easily be replaced with an extra.Skip(1)
after your.SkipWhile()
.
– John
Nov 20 at 2:46
add a comment |
up vote
0
down vote
You are interested in only the substring after colon, so you want to drop the characters before, you need to use skipWhile
:
string test = "ant:man:jack";
var results = test.SkipWhile(t => t != ':').Skip(0).ToList();
Remember skipWhile
will skip characters till the predicate is true, that means at first instance of : it will return a list of chars.
"It will return the substring" - but it doesn't? AList<char>
does not astring
maketh! Also theremoveAt(0)
could easily be replaced with an extra.Skip(1)
after your.SkipWhile()
.
– John
Nov 20 at 2:46
add a comment |
up vote
0
down vote
up vote
0
down vote
You are interested in only the substring after colon, so you want to drop the characters before, you need to use skipWhile
:
string test = "ant:man:jack";
var results = test.SkipWhile(t => t != ':').Skip(0).ToList();
Remember skipWhile
will skip characters till the predicate is true, that means at first instance of : it will return a list of chars.
You are interested in only the substring after colon, so you want to drop the characters before, you need to use skipWhile
:
string test = "ant:man:jack";
var results = test.SkipWhile(t => t != ':').Skip(0).ToList();
Remember skipWhile
will skip characters till the predicate is true, that means at first instance of : it will return a list of chars.
edited Nov 20 at 2:50
answered Nov 20 at 2:02
peeyush singh
352210
352210
"It will return the substring" - but it doesn't? AList<char>
does not astring
maketh! Also theremoveAt(0)
could easily be replaced with an extra.Skip(1)
after your.SkipWhile()
.
– John
Nov 20 at 2:46
add a comment |
"It will return the substring" - but it doesn't? AList<char>
does not astring
maketh! Also theremoveAt(0)
could easily be replaced with an extra.Skip(1)
after your.SkipWhile()
.
– John
Nov 20 at 2:46
"It will return the substring" - but it doesn't? A
List<char>
does not a string
maketh! Also the removeAt(0)
could easily be replaced with an extra .Skip(1)
after your .SkipWhile()
.– John
Nov 20 at 2:46
"It will return the substring" - but it doesn't? A
List<char>
does not a string
maketh! Also the removeAt(0)
could easily be replaced with an extra .Skip(1)
after your .SkipWhile()
.– John
Nov 20 at 2:46
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385088%2fsplit-string-with-character%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
5
Why do you need another method? What's wrong with your current method?
– John
Nov 20 at 1:47
2
Substring(var.IndexOf(':') + 1);
will be the fastest and least verbose, what is your actual problem why cant you use it– TheGeneral
Nov 20 at 2:03
@TheGeneral the question was only live 30 minutes before your comment. Can we be fair and recognize that people may post and step away for a meeting, dinner, time with family, sleep?
– stealththeninja
Nov 20 at 2:41
@stealththeninja if you add the appropriate information ill be more than happy to remove the votes.
– TheGeneral
Nov 20 at 2:44
do you need both strings ("ant" and "man : jack") as a result?
– vasily.sib
Nov 20 at 3:27