C# IndexOf, when word is part of another word, How to?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
let's say I have string "soak oak"
and I want to have string index
of ("oak"
), it returns me the index of where "oak"
starts in "soak"
(1) but I want to find index of exact word "oak"
(5), what do I need to do?
string text = "soak oak";
char seperators = {' ', '.', ',', '!', '?', ':',
';', '(', ')', 't', 'r', 'n', '"', '„', '“'};
string parts = text.Split(seperators,
StringSplitOptions.RemoveEmptyEntries);
text.IndexOf("oak"); // gets '1' because "oak" is in "soak"
// but I want to get 5 because of exact word "oak"
c# string
add a comment |
let's say I have string "soak oak"
and I want to have string index
of ("oak"
), it returns me the index of where "oak"
starts in "soak"
(1) but I want to find index of exact word "oak"
(5), what do I need to do?
string text = "soak oak";
char seperators = {' ', '.', ',', '!', '?', ':',
';', '(', ')', 't', 'r', 'n', '"', '„', '“'};
string parts = text.Split(seperators,
StringSplitOptions.RemoveEmptyEntries);
text.IndexOf("oak"); // gets '1' because "oak" is in "soak"
// but I want to get 5 because of exact word "oak"
c# string
2
You are searching inside the stringtext
instead of the stringparts
.
– Peter B
Nov 22 '18 at 10:59
2
you're most of the way there... You've split the text by separators into parts. Now you just need to check if any of those parts are exactly "oak"
– James
Nov 22 '18 at 10:59
You need to implement an algorithm for it.Step 1. Push all characters into an array, Step 2. Check first letter of the array or letter after a space is starting with the keyword you are looking for. step 3: On start letter char match, loop for consecutive char to match the rest of the keywords. step 4: if all char match found then the first char index is your required ANSWER!!
– Pranesh Janarthanan
Nov 22 '18 at 11:08
You could make use of the System.Text.RegularExpression namespace. Here is an example of matching a whole word using Regex: regex101.com/r/7ijsEh/1 . You can then use Regex.Match(text, "boakb").Index. See here: dotnetfiddle.net/94XDQb
– DParry
Nov 22 '18 at 11:15
add a comment |
let's say I have string "soak oak"
and I want to have string index
of ("oak"
), it returns me the index of where "oak"
starts in "soak"
(1) but I want to find index of exact word "oak"
(5), what do I need to do?
string text = "soak oak";
char seperators = {' ', '.', ',', '!', '?', ':',
';', '(', ')', 't', 'r', 'n', '"', '„', '“'};
string parts = text.Split(seperators,
StringSplitOptions.RemoveEmptyEntries);
text.IndexOf("oak"); // gets '1' because "oak" is in "soak"
// but I want to get 5 because of exact word "oak"
c# string
let's say I have string "soak oak"
and I want to have string index
of ("oak"
), it returns me the index of where "oak"
starts in "soak"
(1) but I want to find index of exact word "oak"
(5), what do I need to do?
string text = "soak oak";
char seperators = {' ', '.', ',', '!', '?', ':',
';', '(', ')', 't', 'r', 'n', '"', '„', '“'};
string parts = text.Split(seperators,
StringSplitOptions.RemoveEmptyEntries);
text.IndexOf("oak"); // gets '1' because "oak" is in "soak"
// but I want to get 5 because of exact word "oak"
c# string
c# string
edited Nov 22 '18 at 11:40
Dmitry Bychenko
112k1099142
112k1099142
asked Nov 22 '18 at 10:56
GoodnightJessGoodnightJess
255
255
2
You are searching inside the stringtext
instead of the stringparts
.
– Peter B
Nov 22 '18 at 10:59
2
you're most of the way there... You've split the text by separators into parts. Now you just need to check if any of those parts are exactly "oak"
– James
Nov 22 '18 at 10:59
You need to implement an algorithm for it.Step 1. Push all characters into an array, Step 2. Check first letter of the array or letter after a space is starting with the keyword you are looking for. step 3: On start letter char match, loop for consecutive char to match the rest of the keywords. step 4: if all char match found then the first char index is your required ANSWER!!
– Pranesh Janarthanan
Nov 22 '18 at 11:08
You could make use of the System.Text.RegularExpression namespace. Here is an example of matching a whole word using Regex: regex101.com/r/7ijsEh/1 . You can then use Regex.Match(text, "boakb").Index. See here: dotnetfiddle.net/94XDQb
– DParry
Nov 22 '18 at 11:15
add a comment |
2
You are searching inside the stringtext
instead of the stringparts
.
– Peter B
Nov 22 '18 at 10:59
2
you're most of the way there... You've split the text by separators into parts. Now you just need to check if any of those parts are exactly "oak"
– James
Nov 22 '18 at 10:59
You need to implement an algorithm for it.Step 1. Push all characters into an array, Step 2. Check first letter of the array or letter after a space is starting with the keyword you are looking for. step 3: On start letter char match, loop for consecutive char to match the rest of the keywords. step 4: if all char match found then the first char index is your required ANSWER!!
– Pranesh Janarthanan
Nov 22 '18 at 11:08
You could make use of the System.Text.RegularExpression namespace. Here is an example of matching a whole word using Regex: regex101.com/r/7ijsEh/1 . You can then use Regex.Match(text, "boakb").Index. See here: dotnetfiddle.net/94XDQb
– DParry
Nov 22 '18 at 11:15
2
2
You are searching inside the string
text
instead of the string parts
.– Peter B
Nov 22 '18 at 10:59
You are searching inside the string
text
instead of the string parts
.– Peter B
Nov 22 '18 at 10:59
2
2
you're most of the way there... You've split the text by separators into parts. Now you just need to check if any of those parts are exactly "oak"
– James
Nov 22 '18 at 10:59
you're most of the way there... You've split the text by separators into parts. Now you just need to check if any of those parts are exactly "oak"
– James
Nov 22 '18 at 10:59
You need to implement an algorithm for it.Step 1. Push all characters into an array, Step 2. Check first letter of the array or letter after a space is starting with the keyword you are looking for. step 3: On start letter char match, loop for consecutive char to match the rest of the keywords. step 4: if all char match found then the first char index is your required ANSWER!!
– Pranesh Janarthanan
Nov 22 '18 at 11:08
You need to implement an algorithm for it.Step 1. Push all characters into an array, Step 2. Check first letter of the array or letter after a space is starting with the keyword you are looking for. step 3: On start letter char match, loop for consecutive char to match the rest of the keywords. step 4: if all char match found then the first char index is your required ANSWER!!
– Pranesh Janarthanan
Nov 22 '18 at 11:08
You could make use of the System.Text.RegularExpression namespace. Here is an example of matching a whole word using Regex: regex101.com/r/7ijsEh/1 . You can then use Regex.Match(text, "boakb").Index. See here: dotnetfiddle.net/94XDQb
– DParry
Nov 22 '18 at 11:15
You could make use of the System.Text.RegularExpression namespace. Here is an example of matching a whole word using Regex: regex101.com/r/7ijsEh/1 . You can then use Regex.Match(text, "boakb").Index. See here: dotnetfiddle.net/94XDQb
– DParry
Nov 22 '18 at 11:15
add a comment |
6 Answers
6
active
oldest
votes
We can test indexes (IndexOf
) in a loop:
static HashSet<char> s_Separtors = new HashSet<char>() {
' ', '.', ',', '!', '?', ':', ';', '(', ')', 't', 'r', 'n', '"', '„', '“'
};
private static int WordIndexOf(string source, string toFind) {
if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(toFind))
return -1;
for (int index = source.IndexOf(toFind);
index >= 0;
index = source.IndexOf(toFind, index + 1)) {
if (index < 0)
return -1;
if ((index == 0 || s_Separtors.Contains(source[index - 1])) &&
(index >= source.Length - toFind.Length ||
s_Separtors.Contains(source[index + toFind.Length])))
return index;
}
return -1;
}
Demo:
// 5
Console.Write(WordIndexOf("soak oak", "oak"));
add a comment |
Regex approach
string text = "soak oak";
int result = Regex.Match(text, @"boakb").Index;
1
or you could use "boakb", which looks for word boundaries
– Robin Bennett
Nov 22 '18 at 11:08
string text = "soak oak"; var match = Regex.Match(text, @"s[oak]"); if (match.Success) { Console.WriteLine(match.Index); // 4 }
– Aakarsh Dhawan
Nov 22 '18 at 11:14
string text = "I can see antoak."
Expected: 13, actual: 0
– Dmitry Bychenko
Nov 22 '18 at 11:16
@RobinBennett thanks, I've updated my code to your suggestion
– fubo
Nov 22 '18 at 12:18
add a comment |
You may use below regex to find exact word in your string.
string text = "soak oak";
string searchText = "oak";
var index = Regex.Match(text, @"b" + Regex.Escape(searchText) + @"b").Index;
Output:
5
See the demo
@"b" + Regex.Escape(searchText) + @"b"
to be on the safe side
– Dmitry Bychenko
Nov 22 '18 at 11:18
@DmitryBychenko, thanks for improvement, answer updated :)
– er-sho
Nov 22 '18 at 11:19
add a comment |
You can use regular expressions, you may also find it useful to use word boundaries defined by regular expressions:
string text = "soak oak";
var pattern = @"boakb";
var regex = new Regex(pattern);
foreach(Match m in regex.Matches(text)){
Console.WriteLine(m.Index);
Console.WriteLine(m.Value);
}
add a comment |
You could find the string in your array by converting it to a list and using the IndexOf() method.
parts.ToList().IndexOf("oak");
That tells you which array item it is, rather than the index in the original string.
add a comment |
Another RegEx approach-
string text = "soak oak";
var match = Regex.Match(text, @"s[oak]");
if (match.Success)
{
Console.WriteLine(match.Index); // 4
}
- s White space
Hope it helps.
[oak]
is inccorect here: counter example is"a kao is not an oak"
– Dmitry Bychenko
Nov 22 '18 at 11:19
@DmitryBychenko Yes, you are correct. Thanks. How about this though, @"s(oak)"?
– Aakarsh Dhawan
Nov 22 '18 at 11:40
"oak"
is a counter example for@"s(oak)"
: we don't want explicits
in the pattern; please, have a look at er-shoaib's answer
– Dmitry Bychenko
Nov 22 '18 at 11:42
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%2f53429394%2fc-sharp-indexof-when-word-is-part-of-another-word-how-to%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
We can test indexes (IndexOf
) in a loop:
static HashSet<char> s_Separtors = new HashSet<char>() {
' ', '.', ',', '!', '?', ':', ';', '(', ')', 't', 'r', 'n', '"', '„', '“'
};
private static int WordIndexOf(string source, string toFind) {
if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(toFind))
return -1;
for (int index = source.IndexOf(toFind);
index >= 0;
index = source.IndexOf(toFind, index + 1)) {
if (index < 0)
return -1;
if ((index == 0 || s_Separtors.Contains(source[index - 1])) &&
(index >= source.Length - toFind.Length ||
s_Separtors.Contains(source[index + toFind.Length])))
return index;
}
return -1;
}
Demo:
// 5
Console.Write(WordIndexOf("soak oak", "oak"));
add a comment |
We can test indexes (IndexOf
) in a loop:
static HashSet<char> s_Separtors = new HashSet<char>() {
' ', '.', ',', '!', '?', ':', ';', '(', ')', 't', 'r', 'n', '"', '„', '“'
};
private static int WordIndexOf(string source, string toFind) {
if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(toFind))
return -1;
for (int index = source.IndexOf(toFind);
index >= 0;
index = source.IndexOf(toFind, index + 1)) {
if (index < 0)
return -1;
if ((index == 0 || s_Separtors.Contains(source[index - 1])) &&
(index >= source.Length - toFind.Length ||
s_Separtors.Contains(source[index + toFind.Length])))
return index;
}
return -1;
}
Demo:
// 5
Console.Write(WordIndexOf("soak oak", "oak"));
add a comment |
We can test indexes (IndexOf
) in a loop:
static HashSet<char> s_Separtors = new HashSet<char>() {
' ', '.', ',', '!', '?', ':', ';', '(', ')', 't', 'r', 'n', '"', '„', '“'
};
private static int WordIndexOf(string source, string toFind) {
if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(toFind))
return -1;
for (int index = source.IndexOf(toFind);
index >= 0;
index = source.IndexOf(toFind, index + 1)) {
if (index < 0)
return -1;
if ((index == 0 || s_Separtors.Contains(source[index - 1])) &&
(index >= source.Length - toFind.Length ||
s_Separtors.Contains(source[index + toFind.Length])))
return index;
}
return -1;
}
Demo:
// 5
Console.Write(WordIndexOf("soak oak", "oak"));
We can test indexes (IndexOf
) in a loop:
static HashSet<char> s_Separtors = new HashSet<char>() {
' ', '.', ',', '!', '?', ':', ';', '(', ')', 't', 'r', 'n', '"', '„', '“'
};
private static int WordIndexOf(string source, string toFind) {
if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(toFind))
return -1;
for (int index = source.IndexOf(toFind);
index >= 0;
index = source.IndexOf(toFind, index + 1)) {
if (index < 0)
return -1;
if ((index == 0 || s_Separtors.Contains(source[index - 1])) &&
(index >= source.Length - toFind.Length ||
s_Separtors.Contains(source[index + toFind.Length])))
return index;
}
return -1;
}
Demo:
// 5
Console.Write(WordIndexOf("soak oak", "oak"));
answered Nov 22 '18 at 11:11
Dmitry BychenkoDmitry Bychenko
112k1099142
112k1099142
add a comment |
add a comment |
Regex approach
string text = "soak oak";
int result = Regex.Match(text, @"boakb").Index;
1
or you could use "boakb", which looks for word boundaries
– Robin Bennett
Nov 22 '18 at 11:08
string text = "soak oak"; var match = Regex.Match(text, @"s[oak]"); if (match.Success) { Console.WriteLine(match.Index); // 4 }
– Aakarsh Dhawan
Nov 22 '18 at 11:14
string text = "I can see antoak."
Expected: 13, actual: 0
– Dmitry Bychenko
Nov 22 '18 at 11:16
@RobinBennett thanks, I've updated my code to your suggestion
– fubo
Nov 22 '18 at 12:18
add a comment |
Regex approach
string text = "soak oak";
int result = Regex.Match(text, @"boakb").Index;
1
or you could use "boakb", which looks for word boundaries
– Robin Bennett
Nov 22 '18 at 11:08
string text = "soak oak"; var match = Regex.Match(text, @"s[oak]"); if (match.Success) { Console.WriteLine(match.Index); // 4 }
– Aakarsh Dhawan
Nov 22 '18 at 11:14
string text = "I can see antoak."
Expected: 13, actual: 0
– Dmitry Bychenko
Nov 22 '18 at 11:16
@RobinBennett thanks, I've updated my code to your suggestion
– fubo
Nov 22 '18 at 12:18
add a comment |
Regex approach
string text = "soak oak";
int result = Regex.Match(text, @"boakb").Index;
Regex approach
string text = "soak oak";
int result = Regex.Match(text, @"boakb").Index;
edited Nov 22 '18 at 12:17
answered Nov 22 '18 at 11:06
fubofubo
31.1k970107
31.1k970107
1
or you could use "boakb", which looks for word boundaries
– Robin Bennett
Nov 22 '18 at 11:08
string text = "soak oak"; var match = Regex.Match(text, @"s[oak]"); if (match.Success) { Console.WriteLine(match.Index); // 4 }
– Aakarsh Dhawan
Nov 22 '18 at 11:14
string text = "I can see antoak."
Expected: 13, actual: 0
– Dmitry Bychenko
Nov 22 '18 at 11:16
@RobinBennett thanks, I've updated my code to your suggestion
– fubo
Nov 22 '18 at 12:18
add a comment |
1
or you could use "boakb", which looks for word boundaries
– Robin Bennett
Nov 22 '18 at 11:08
string text = "soak oak"; var match = Regex.Match(text, @"s[oak]"); if (match.Success) { Console.WriteLine(match.Index); // 4 }
– Aakarsh Dhawan
Nov 22 '18 at 11:14
string text = "I can see antoak."
Expected: 13, actual: 0
– Dmitry Bychenko
Nov 22 '18 at 11:16
@RobinBennett thanks, I've updated my code to your suggestion
– fubo
Nov 22 '18 at 12:18
1
1
or you could use "boakb", which looks for word boundaries
– Robin Bennett
Nov 22 '18 at 11:08
or you could use "boakb", which looks for word boundaries
– Robin Bennett
Nov 22 '18 at 11:08
string text = "soak oak"; var match = Regex.Match(text, @"s[oak]"); if (match.Success) { Console.WriteLine(match.Index); // 4 }
– Aakarsh Dhawan
Nov 22 '18 at 11:14
string text = "soak oak"; var match = Regex.Match(text, @"s[oak]"); if (match.Success) { Console.WriteLine(match.Index); // 4 }
– Aakarsh Dhawan
Nov 22 '18 at 11:14
string text = "I can see antoak."
Expected: 13, actual: 0– Dmitry Bychenko
Nov 22 '18 at 11:16
string text = "I can see antoak."
Expected: 13, actual: 0– Dmitry Bychenko
Nov 22 '18 at 11:16
@RobinBennett thanks, I've updated my code to your suggestion
– fubo
Nov 22 '18 at 12:18
@RobinBennett thanks, I've updated my code to your suggestion
– fubo
Nov 22 '18 at 12:18
add a comment |
You may use below regex to find exact word in your string.
string text = "soak oak";
string searchText = "oak";
var index = Regex.Match(text, @"b" + Regex.Escape(searchText) + @"b").Index;
Output:
5
See the demo
@"b" + Regex.Escape(searchText) + @"b"
to be on the safe side
– Dmitry Bychenko
Nov 22 '18 at 11:18
@DmitryBychenko, thanks for improvement, answer updated :)
– er-sho
Nov 22 '18 at 11:19
add a comment |
You may use below regex to find exact word in your string.
string text = "soak oak";
string searchText = "oak";
var index = Regex.Match(text, @"b" + Regex.Escape(searchText) + @"b").Index;
Output:
5
See the demo
@"b" + Regex.Escape(searchText) + @"b"
to be on the safe side
– Dmitry Bychenko
Nov 22 '18 at 11:18
@DmitryBychenko, thanks for improvement, answer updated :)
– er-sho
Nov 22 '18 at 11:19
add a comment |
You may use below regex to find exact word in your string.
string text = "soak oak";
string searchText = "oak";
var index = Regex.Match(text, @"b" + Regex.Escape(searchText) + @"b").Index;
Output:
5
See the demo
You may use below regex to find exact word in your string.
string text = "soak oak";
string searchText = "oak";
var index = Regex.Match(text, @"b" + Regex.Escape(searchText) + @"b").Index;
Output:
5
See the demo
edited Nov 22 '18 at 11:19
answered Nov 22 '18 at 11:12
er-shoer-sho
7,1402619
7,1402619
@"b" + Regex.Escape(searchText) + @"b"
to be on the safe side
– Dmitry Bychenko
Nov 22 '18 at 11:18
@DmitryBychenko, thanks for improvement, answer updated :)
– er-sho
Nov 22 '18 at 11:19
add a comment |
@"b" + Regex.Escape(searchText) + @"b"
to be on the safe side
– Dmitry Bychenko
Nov 22 '18 at 11:18
@DmitryBychenko, thanks for improvement, answer updated :)
– er-sho
Nov 22 '18 at 11:19
@"b" + Regex.Escape(searchText) + @"b"
to be on the safe side– Dmitry Bychenko
Nov 22 '18 at 11:18
@"b" + Regex.Escape(searchText) + @"b"
to be on the safe side– Dmitry Bychenko
Nov 22 '18 at 11:18
@DmitryBychenko, thanks for improvement, answer updated :)
– er-sho
Nov 22 '18 at 11:19
@DmitryBychenko, thanks for improvement, answer updated :)
– er-sho
Nov 22 '18 at 11:19
add a comment |
You can use regular expressions, you may also find it useful to use word boundaries defined by regular expressions:
string text = "soak oak";
var pattern = @"boakb";
var regex = new Regex(pattern);
foreach(Match m in regex.Matches(text)){
Console.WriteLine(m.Index);
Console.WriteLine(m.Value);
}
add a comment |
You can use regular expressions, you may also find it useful to use word boundaries defined by regular expressions:
string text = "soak oak";
var pattern = @"boakb";
var regex = new Regex(pattern);
foreach(Match m in regex.Matches(text)){
Console.WriteLine(m.Index);
Console.WriteLine(m.Value);
}
add a comment |
You can use regular expressions, you may also find it useful to use word boundaries defined by regular expressions:
string text = "soak oak";
var pattern = @"boakb";
var regex = new Regex(pattern);
foreach(Match m in regex.Matches(text)){
Console.WriteLine(m.Index);
Console.WriteLine(m.Value);
}
You can use regular expressions, you may also find it useful to use word boundaries defined by regular expressions:
string text = "soak oak";
var pattern = @"boakb";
var regex = new Regex(pattern);
foreach(Match m in regex.Matches(text)){
Console.WriteLine(m.Index);
Console.WriteLine(m.Value);
}
answered Nov 22 '18 at 11:11
Muaz OthmanMuaz Othman
362313
362313
add a comment |
add a comment |
You could find the string in your array by converting it to a list and using the IndexOf() method.
parts.ToList().IndexOf("oak");
That tells you which array item it is, rather than the index in the original string.
add a comment |
You could find the string in your array by converting it to a list and using the IndexOf() method.
parts.ToList().IndexOf("oak");
That tells you which array item it is, rather than the index in the original string.
add a comment |
You could find the string in your array by converting it to a list and using the IndexOf() method.
parts.ToList().IndexOf("oak");
That tells you which array item it is, rather than the index in the original string.
You could find the string in your array by converting it to a list and using the IndexOf() method.
parts.ToList().IndexOf("oak");
That tells you which array item it is, rather than the index in the original string.
answered Nov 22 '18 at 11:12
Robin BennettRobin Bennett
1,925413
1,925413
add a comment |
add a comment |
Another RegEx approach-
string text = "soak oak";
var match = Regex.Match(text, @"s[oak]");
if (match.Success)
{
Console.WriteLine(match.Index); // 4
}
- s White space
Hope it helps.
[oak]
is inccorect here: counter example is"a kao is not an oak"
– Dmitry Bychenko
Nov 22 '18 at 11:19
@DmitryBychenko Yes, you are correct. Thanks. How about this though, @"s(oak)"?
– Aakarsh Dhawan
Nov 22 '18 at 11:40
"oak"
is a counter example for@"s(oak)"
: we don't want explicits
in the pattern; please, have a look at er-shoaib's answer
– Dmitry Bychenko
Nov 22 '18 at 11:42
add a comment |
Another RegEx approach-
string text = "soak oak";
var match = Regex.Match(text, @"s[oak]");
if (match.Success)
{
Console.WriteLine(match.Index); // 4
}
- s White space
Hope it helps.
[oak]
is inccorect here: counter example is"a kao is not an oak"
– Dmitry Bychenko
Nov 22 '18 at 11:19
@DmitryBychenko Yes, you are correct. Thanks. How about this though, @"s(oak)"?
– Aakarsh Dhawan
Nov 22 '18 at 11:40
"oak"
is a counter example for@"s(oak)"
: we don't want explicits
in the pattern; please, have a look at er-shoaib's answer
– Dmitry Bychenko
Nov 22 '18 at 11:42
add a comment |
Another RegEx approach-
string text = "soak oak";
var match = Regex.Match(text, @"s[oak]");
if (match.Success)
{
Console.WriteLine(match.Index); // 4
}
- s White space
Hope it helps.
Another RegEx approach-
string text = "soak oak";
var match = Regex.Match(text, @"s[oak]");
if (match.Success)
{
Console.WriteLine(match.Index); // 4
}
- s White space
Hope it helps.
answered Nov 22 '18 at 11:13
Aakarsh DhawanAakarsh Dhawan
1196
1196
[oak]
is inccorect here: counter example is"a kao is not an oak"
– Dmitry Bychenko
Nov 22 '18 at 11:19
@DmitryBychenko Yes, you are correct. Thanks. How about this though, @"s(oak)"?
– Aakarsh Dhawan
Nov 22 '18 at 11:40
"oak"
is a counter example for@"s(oak)"
: we don't want explicits
in the pattern; please, have a look at er-shoaib's answer
– Dmitry Bychenko
Nov 22 '18 at 11:42
add a comment |
[oak]
is inccorect here: counter example is"a kao is not an oak"
– Dmitry Bychenko
Nov 22 '18 at 11:19
@DmitryBychenko Yes, you are correct. Thanks. How about this though, @"s(oak)"?
– Aakarsh Dhawan
Nov 22 '18 at 11:40
"oak"
is a counter example for@"s(oak)"
: we don't want explicits
in the pattern; please, have a look at er-shoaib's answer
– Dmitry Bychenko
Nov 22 '18 at 11:42
[oak]
is inccorect here: counter example is "a kao is not an oak"
– Dmitry Bychenko
Nov 22 '18 at 11:19
[oak]
is inccorect here: counter example is "a kao is not an oak"
– Dmitry Bychenko
Nov 22 '18 at 11:19
@DmitryBychenko Yes, you are correct. Thanks. How about this though, @"s(oak)"?
– Aakarsh Dhawan
Nov 22 '18 at 11:40
@DmitryBychenko Yes, you are correct. Thanks. How about this though, @"s(oak)"?
– Aakarsh Dhawan
Nov 22 '18 at 11:40
"oak"
is a counter example for @"s(oak)"
: we don't want explicit s
in the pattern; please, have a look at er-shoaib's answer– Dmitry Bychenko
Nov 22 '18 at 11:42
"oak"
is a counter example for @"s(oak)"
: we don't want explicit s
in the pattern; please, have a look at er-shoaib's answer– Dmitry Bychenko
Nov 22 '18 at 11:42
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%2f53429394%2fc-sharp-indexof-when-word-is-part-of-another-word-how-to%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
2
You are searching inside the string
text
instead of the stringparts
.– Peter B
Nov 22 '18 at 10:59
2
you're most of the way there... You've split the text by separators into parts. Now you just need to check if any of those parts are exactly "oak"
– James
Nov 22 '18 at 10:59
You need to implement an algorithm for it.Step 1. Push all characters into an array, Step 2. Check first letter of the array or letter after a space is starting with the keyword you are looking for. step 3: On start letter char match, loop for consecutive char to match the rest of the keywords. step 4: if all char match found then the first char index is your required ANSWER!!
– Pranesh Janarthanan
Nov 22 '18 at 11:08
You could make use of the System.Text.RegularExpression namespace. Here is an example of matching a whole word using Regex: regex101.com/r/7ijsEh/1 . You can then use Regex.Match(text, "boakb").Index. See here: dotnetfiddle.net/94XDQb
– DParry
Nov 22 '18 at 11:15