Going to the end of a substring in c#
The comment // go to end, I can't figure out how to cleanly end the substring :(
Is there a simpler way to go to the end of the substring rather than mathing out the number by myself? For more complex strings this would be too hard
string word = Console.ReadLine();
string lines = File.ReadAllLines(file);
using (var far = File.CreateText(resultfile))
{
foreach (string line in lines)
{
StringBuilder NewL = new StringBuilder();
int ind = line.IndexOf(word);
if (ind >= 0)
{
if (ind == 0)
{
NewL.Append(line.Substring(ind+ word.Length +1, // go to end);
}else{
NewL.Append(line.Substring(0, ind - 1));
NewL.Append(line.Substring(ind + word.Length + 1, // go to end));}
far.WriteLine(NewL);
}
else
{
far.WriteLine(line);
}
}
I don't know what more details the stackoverflow wants, anyone who can answer this pretty sure can clearly understand this simple code anyways.
c# string substring
add a comment |
The comment // go to end, I can't figure out how to cleanly end the substring :(
Is there a simpler way to go to the end of the substring rather than mathing out the number by myself? For more complex strings this would be too hard
string word = Console.ReadLine();
string lines = File.ReadAllLines(file);
using (var far = File.CreateText(resultfile))
{
foreach (string line in lines)
{
StringBuilder NewL = new StringBuilder();
int ind = line.IndexOf(word);
if (ind >= 0)
{
if (ind == 0)
{
NewL.Append(line.Substring(ind+ word.Length +1, // go to end);
}else{
NewL.Append(line.Substring(0, ind - 1));
NewL.Append(line.Substring(ind + word.Length + 1, // go to end));}
far.WriteLine(NewL);
}
else
{
far.WriteLine(line);
}
}
I don't know what more details the stackoverflow wants, anyone who can answer this pretty sure can clearly understand this simple code anyways.
c# string substring
Are you just trying to remove a certain word from the input lines loaded from a file and then rewrite the lines?
– Steve
Nov 22 '18 at 22:04
add a comment |
The comment // go to end, I can't figure out how to cleanly end the substring :(
Is there a simpler way to go to the end of the substring rather than mathing out the number by myself? For more complex strings this would be too hard
string word = Console.ReadLine();
string lines = File.ReadAllLines(file);
using (var far = File.CreateText(resultfile))
{
foreach (string line in lines)
{
StringBuilder NewL = new StringBuilder();
int ind = line.IndexOf(word);
if (ind >= 0)
{
if (ind == 0)
{
NewL.Append(line.Substring(ind+ word.Length +1, // go to end);
}else{
NewL.Append(line.Substring(0, ind - 1));
NewL.Append(line.Substring(ind + word.Length + 1, // go to end));}
far.WriteLine(NewL);
}
else
{
far.WriteLine(line);
}
}
I don't know what more details the stackoverflow wants, anyone who can answer this pretty sure can clearly understand this simple code anyways.
c# string substring
The comment // go to end, I can't figure out how to cleanly end the substring :(
Is there a simpler way to go to the end of the substring rather than mathing out the number by myself? For more complex strings this would be too hard
string word = Console.ReadLine();
string lines = File.ReadAllLines(file);
using (var far = File.CreateText(resultfile))
{
foreach (string line in lines)
{
StringBuilder NewL = new StringBuilder();
int ind = line.IndexOf(word);
if (ind >= 0)
{
if (ind == 0)
{
NewL.Append(line.Substring(ind+ word.Length +1, // go to end);
}else{
NewL.Append(line.Substring(0, ind - 1));
NewL.Append(line.Substring(ind + word.Length + 1, // go to end));}
far.WriteLine(NewL);
}
else
{
far.WriteLine(line);
}
}
I don't know what more details the stackoverflow wants, anyone who can answer this pretty sure can clearly understand this simple code anyways.
c# string substring
c# string substring
asked Nov 22 '18 at 21:58
S.KikkS.Kikk
31
31
Are you just trying to remove a certain word from the input lines loaded from a file and then rewrite the lines?
– Steve
Nov 22 '18 at 22:04
add a comment |
Are you just trying to remove a certain word from the input lines loaded from a file and then rewrite the lines?
– Steve
Nov 22 '18 at 22:04
Are you just trying to remove a certain word from the input lines loaded from a file and then rewrite the lines?
– Steve
Nov 22 '18 at 22:04
Are you just trying to remove a certain word from the input lines loaded from a file and then rewrite the lines?
– Steve
Nov 22 '18 at 22:04
add a comment |
2 Answers
2
active
oldest
votes
You can use the String.Substring(int)
overload, which automatically continues to the end of the source string:
NewL.Append(line.Substring(ind + word.Length + 1));
Retrieves a substring from this instance. The substring starts at a specified character position and continues to the end of the string.
I tried this before , but didn't notice that I forgot a " ) " at the end ... Thanks
– S.Kikk
Nov 22 '18 at 22:07
add a comment |
It seems to me that you are just trying to remove a certain word from the loaded lines. If this is your task then you can simply replace the word with an empty string
foreach (string line in lines)
{
string newLine = line.Replace(word, "");
far.WriteLine(newLine);
}
Or even without an explicit loop with a bit of Linq
var result = lines.Select(x = x.Replace(word,""));
File.WriteAllLines("yourFile.txt", result);
Or, given the requirement to match an additional character after the word you can solve it with Regex.
Regex r = new Regex(word + ".");
var result = lines.Select(x => r.Replace(x, ""));
File.WriteAllLines("yourFile.txt", result);
Have to replace word + 1 character after it with nothing
– S.Kikk
Nov 22 '18 at 22:11
I see, then perhaps Regex could help.
– Steve
Nov 22 '18 at 22:12
Yeah I already finished this with Regex, but trying to see if it's possible for me to do without it. Thanks
– S.Kikk
Nov 22 '18 at 22:18
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',
autoActivateHeartbeat: false,
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%2f53438403%2fgoing-to-the-end-of-a-substring-in-c-sharp%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
You can use the String.Substring(int)
overload, which automatically continues to the end of the source string:
NewL.Append(line.Substring(ind + word.Length + 1));
Retrieves a substring from this instance. The substring starts at a specified character position and continues to the end of the string.
I tried this before , but didn't notice that I forgot a " ) " at the end ... Thanks
– S.Kikk
Nov 22 '18 at 22:07
add a comment |
You can use the String.Substring(int)
overload, which automatically continues to the end of the source string:
NewL.Append(line.Substring(ind + word.Length + 1));
Retrieves a substring from this instance. The substring starts at a specified character position and continues to the end of the string.
I tried this before , but didn't notice that I forgot a " ) " at the end ... Thanks
– S.Kikk
Nov 22 '18 at 22:07
add a comment |
You can use the String.Substring(int)
overload, which automatically continues to the end of the source string:
NewL.Append(line.Substring(ind + word.Length + 1));
Retrieves a substring from this instance. The substring starts at a specified character position and continues to the end of the string.
You can use the String.Substring(int)
overload, which automatically continues to the end of the source string:
NewL.Append(line.Substring(ind + word.Length + 1));
Retrieves a substring from this instance. The substring starts at a specified character position and continues to the end of the string.
answered Nov 22 '18 at 22:02
DouglasDouglas
43.2k689138
43.2k689138
I tried this before , but didn't notice that I forgot a " ) " at the end ... Thanks
– S.Kikk
Nov 22 '18 at 22:07
add a comment |
I tried this before , but didn't notice that I forgot a " ) " at the end ... Thanks
– S.Kikk
Nov 22 '18 at 22:07
I tried this before , but didn't notice that I forgot a " ) " at the end ... Thanks
– S.Kikk
Nov 22 '18 at 22:07
I tried this before , but didn't notice that I forgot a " ) " at the end ... Thanks
– S.Kikk
Nov 22 '18 at 22:07
add a comment |
It seems to me that you are just trying to remove a certain word from the loaded lines. If this is your task then you can simply replace the word with an empty string
foreach (string line in lines)
{
string newLine = line.Replace(word, "");
far.WriteLine(newLine);
}
Or even without an explicit loop with a bit of Linq
var result = lines.Select(x = x.Replace(word,""));
File.WriteAllLines("yourFile.txt", result);
Or, given the requirement to match an additional character after the word you can solve it with Regex.
Regex r = new Regex(word + ".");
var result = lines.Select(x => r.Replace(x, ""));
File.WriteAllLines("yourFile.txt", result);
Have to replace word + 1 character after it with nothing
– S.Kikk
Nov 22 '18 at 22:11
I see, then perhaps Regex could help.
– Steve
Nov 22 '18 at 22:12
Yeah I already finished this with Regex, but trying to see if it's possible for me to do without it. Thanks
– S.Kikk
Nov 22 '18 at 22:18
add a comment |
It seems to me that you are just trying to remove a certain word from the loaded lines. If this is your task then you can simply replace the word with an empty string
foreach (string line in lines)
{
string newLine = line.Replace(word, "");
far.WriteLine(newLine);
}
Or even without an explicit loop with a bit of Linq
var result = lines.Select(x = x.Replace(word,""));
File.WriteAllLines("yourFile.txt", result);
Or, given the requirement to match an additional character after the word you can solve it with Regex.
Regex r = new Regex(word + ".");
var result = lines.Select(x => r.Replace(x, ""));
File.WriteAllLines("yourFile.txt", result);
Have to replace word + 1 character after it with nothing
– S.Kikk
Nov 22 '18 at 22:11
I see, then perhaps Regex could help.
– Steve
Nov 22 '18 at 22:12
Yeah I already finished this with Regex, but trying to see if it's possible for me to do without it. Thanks
– S.Kikk
Nov 22 '18 at 22:18
add a comment |
It seems to me that you are just trying to remove a certain word from the loaded lines. If this is your task then you can simply replace the word with an empty string
foreach (string line in lines)
{
string newLine = line.Replace(word, "");
far.WriteLine(newLine);
}
Or even without an explicit loop with a bit of Linq
var result = lines.Select(x = x.Replace(word,""));
File.WriteAllLines("yourFile.txt", result);
Or, given the requirement to match an additional character after the word you can solve it with Regex.
Regex r = new Regex(word + ".");
var result = lines.Select(x => r.Replace(x, ""));
File.WriteAllLines("yourFile.txt", result);
It seems to me that you are just trying to remove a certain word from the loaded lines. If this is your task then you can simply replace the word with an empty string
foreach (string line in lines)
{
string newLine = line.Replace(word, "");
far.WriteLine(newLine);
}
Or even without an explicit loop with a bit of Linq
var result = lines.Select(x = x.Replace(word,""));
File.WriteAllLines("yourFile.txt", result);
Or, given the requirement to match an additional character after the word you can solve it with Regex.
Regex r = new Regex(word + ".");
var result = lines.Select(x => r.Replace(x, ""));
File.WriteAllLines("yourFile.txt", result);
edited Nov 22 '18 at 22:19
answered Nov 22 '18 at 22:07
SteveSteve
182k16157221
182k16157221
Have to replace word + 1 character after it with nothing
– S.Kikk
Nov 22 '18 at 22:11
I see, then perhaps Regex could help.
– Steve
Nov 22 '18 at 22:12
Yeah I already finished this with Regex, but trying to see if it's possible for me to do without it. Thanks
– S.Kikk
Nov 22 '18 at 22:18
add a comment |
Have to replace word + 1 character after it with nothing
– S.Kikk
Nov 22 '18 at 22:11
I see, then perhaps Regex could help.
– Steve
Nov 22 '18 at 22:12
Yeah I already finished this with Regex, but trying to see if it's possible for me to do without it. Thanks
– S.Kikk
Nov 22 '18 at 22:18
Have to replace word + 1 character after it with nothing
– S.Kikk
Nov 22 '18 at 22:11
Have to replace word + 1 character after it with nothing
– S.Kikk
Nov 22 '18 at 22:11
I see, then perhaps Regex could help.
– Steve
Nov 22 '18 at 22:12
I see, then perhaps Regex could help.
– Steve
Nov 22 '18 at 22:12
Yeah I already finished this with Regex, but trying to see if it's possible for me to do without it. Thanks
– S.Kikk
Nov 22 '18 at 22:18
Yeah I already finished this with Regex, but trying to see if it's possible for me to do without it. Thanks
– S.Kikk
Nov 22 '18 at 22:18
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.
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%2f53438403%2fgoing-to-the-end-of-a-substring-in-c-sharp%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
Are you just trying to remove a certain word from the input lines loaded from a file and then rewrite the lines?
– Steve
Nov 22 '18 at 22:04