Going to the end of a substring in c#












0















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.










share|improve this question























  • 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
















0















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.










share|improve this question























  • 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














0












0








0








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.










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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



















  • 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












2 Answers
2






active

oldest

votes


















4














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.







share|improve this answer
























  • I tried this before , but didn't notice that I forgot a " ) " at the end ... Thanks

    – S.Kikk
    Nov 22 '18 at 22:07



















2














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);





share|improve this answer


























  • 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











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
});


}
});














draft saved

draft discarded


















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









4














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.







share|improve this answer
























  • I tried this before , but didn't notice that I forgot a " ) " at the end ... Thanks

    – S.Kikk
    Nov 22 '18 at 22:07
















4














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.







share|improve this answer
























  • I tried this before , but didn't notice that I forgot a " ) " at the end ... Thanks

    – S.Kikk
    Nov 22 '18 at 22:07














4












4








4







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.







share|improve this answer













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.








share|improve this answer












share|improve this answer



share|improve this answer










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



















  • 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













2














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);





share|improve this answer


























  • 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
















2














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);





share|improve this answer


























  • 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














2












2








2







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);





share|improve this answer















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);






share|improve this answer














share|improve this answer



share|improve this answer








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



















  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

If I really need a card on my start hand, how many mulligans make sense? [duplicate]

Alcedinidae

Can an atomic nucleus contain both particles and antiparticles? [duplicate]