c# find keywords in a string and remove it [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have got a list with keywords. And I coded a method that if a string contains keyword from list, the method must remove keyword from string. Here is the method:
private string RemoveFromList(string sentence)
{
var lists = new List<string>{ "ask-", "que-", "(app)", "(exe)", "(foo)" };
var control = lists.Any(sentence.Contains);
string result;
if (control)
{
var index = sentence.IndexOf(lists.FirstOrDefault(sentence.Contains)
?? throw new InvalidOperationException(), StringComparison.Ordinal);
result = index != -1 ? sentence.Remove(index) : sentence;
}
else
result = sentence;
return result;
}
var str = "ask- This is a sentence.";
Message.Box(RemoveFromList(str));
// It does not give to me: This is a sentence.
This method does not work properly. It does not remove the keyword from the string.

Using string.Replace is the simplest approach:
foreach (var word in lists)
{
sentence = sentence.Replace(word,"").Trim();
}
Although that will find the word in the middle of the string too. If you wanted to remove it only at the start you could use IndexOf check it's 0 and then take the string starting from word.Length using Substring. Or use StartsWith:
foreach (var word in lists)
{
if (sentence.StartsWith(word))
{
sentence = sentence.Substring(word.Length).Trim();
// break; // if only one
}
}

There are 2 options for you.
First of all the Remove usage is incorrect. You just want to remove the keyword. If u pass 1 argument to remove it will remove from that index till end. Pass the length of keyword as second arg to Remove.
s.Remove(index, len);
If string contains it than replace the occurrence of keyword with empty string
s.Replace("keyword", "");

Another option is you could create an extension since you already know what items to remove.
using System.Text.RegularExpressions;
public static string RemoveFromList(this string sentence)
{
new List<string>{ "ask-",
"que-",
"(app)",
"(exe)",
"(foo)" }.ForEach(name =>
{
sentence = Regex.Replace(sentence.Replace(name, string.Empty), " {2,}", " ");
});
return sentence;
}
Useage
var str = "ask- This is (app) a que- sentence.".RemoveFromList();
Note
I used Regex.Replace as it's possible you may have some blank spaces floating around after you remove the bad string/s, this helps ensure that doesn't happen.

Related

How to remove duplicates from a string? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
The code is supposed to put all letters into lower case and change j to i, which it does. But I'm trying to take out any duplicate letters.
example inputted string = jjjaaaMMM expected output string = jam
what actually happens real output string = m please help I'm not sure what I'm missing.
string key = Secret.Text;
var keyLow = key.ToLower();
var newKey = keyLow.Replace("j", "i");
var set = new HashSet<char>(newKey);
foreach (char c in set)
{
Secret.Text = Char.ToString(c);
}
Your issue is entirely the = in
Secret.Text = Char.ToString(c);
it needs to be +=
Secret.Text += Char.ToString(c);
You were overwriting each value with the next.
However you could just use linq
Secret.Text = string.Concat(key.ToLower().Replace("j", "i").Distinct());
or probably more efficiently from #Ben Voigt comments
Since you have a sequence of char, it's probably more efficient to
call the string constructor than Concat
Secret.Text = new string(set.ToArray());
// or
Secret.Text = new string(key.ToLower()
.Replace("j", "i")
.Distinct()
.ToArray());
Additional Resources
String.Concat Method
Concatenates one or more instances of String, or the String
representations of the values of one or more instances of Object.
Enumerable.Distinct Method
Returns distinct elements from a sequence.
Others may answer the question directly. I'll provide an alternative. As always with string manipulation, there's a Regex for that.
var output = Regex.Replace(input, #"(.)\1*", "$1");
You can use Linq approach:
var text = key.ToLower().Distinct().ToArray();
Don't forgot add using System.Linq

If string.Contains two numbers + "x" + one number then [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm creating a software about TV series. I need to get the "episode number" from the filename (a string) of the episode.
Examples of filenames:
"Title of the episode 2x04"
"I am another title S05E22"
"Amazing tv serie 22x2"
I thought about splitting the title part from the episode part, but the format of the filename is not the same everytime (eg. nxmm,SnnEmm, nnxm).
Here's my simple code:
foreach (string filename in path) {
if (filename.Contains(*season n episode m*)) {
// do something
}
}
you should use regex for this:
var list = new List<string>();
list.Add("Lost 04x01");
list.Add("Lost 04x02");
list.Add("Lost 4x3");
list.Add("Lost 4x04");
list.Add("Lost S04E05");
list.Add("Lost S04E6");
Regex regex = new Regex(#"S?\d{1,2}[X|E]?\d{1,2}", RegexOptions.IgnoreCase);
foreach (string file in list) {
if (regex.IsMatch(file))
{
// true
}
}
regex101 Demo
Here is the code
string one = "04x22";
string two = "s04e22";
if ((one.Split('x')).Count == 1)
{
string[] res = (one.Split('e'));
// result is res[1]
}
else
{
string[] res = (one.Split('x'));
// result is res[1]
}
Use a regular expression:
var regex = new Regex(#"(?<season>\d+)[Ex](?<episode>\d+)");
foreach (string file in path) {
var match = regex.Match(file);
if (match.Success) {
Console.WriteLine(match.Groups["season"].Value);
Console.WriteLine(match.Groups["episode"].Value);
}
}
This one will match 04x22, 4x22, S04E22, etc. You should be able to tailor it to be more or less restrictive as you require.
You would need to use a regular expression to find the season and episode number from the filename.
Something like (\d?\d)[xE](\d\d). The first group would be the season number, the second - the episode number. See the regex fiddle on explanation of the regex and a demo.
You would need to use Regex.Match() method in your code instead of Contains().

c# how to get specific string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hi I am facing problem to get specific string. The string as below:
string myPurseBalance = "Purse Balance: +0000004000 556080";
I want to get 4000 out only.
if your string format/pattern is fix then you can get specific value
string myPurseBalance = "Purse Balance: +0000004000 556080";
//
var newPursebal =Convert.ToDouble(myPurseBalance.Split('+')[1].Split(' ')[0]);
You can use this regular expression:
string extract = Regex.Replace(myPurseBalance, #"(.*?)\: \+[0]*(?<val>\d+) (.*)", "${val}")
It searches for the decimals after :, trims the leading 0s and removes everything after the last whitespace.
You can use string.Split to get the +0000004000 and then use string.Substring to get the last four characters by passing the Length-4 as starting index.
string str = myPurseBalance.Split(' ')[2];
str = str.Substring(str.Length-4);
Learn Regex . Here is just simple tutorial
using System;
using System.Text.RegularExpressions;
namespace regex
{
class MainClass
{
public static void Main (string[] args)
{
string input = "Purse Balance: +0000504000 556080";
// Here we call Regex.Match.
Match match = Regex.Match(input, #"\+[0]*(\d+)",
RegexOptions.IgnoreCase);
// Here we check the Match instance.
if (match.Success)
{
// Finally, we get the Group value and display it.
string key = match.Groups[1].Value;
Console.WriteLine(key);
}
}
}
}

C# - split string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have some problems with split and check string.
I need to split string, replace halfs and check is this the same as the second string.
example: first string = tokyo second string = koyto
soo... S = a+b = b+a
S - a = b and S - b = a
a and b is part of one string (S) and may have different long in this case a = to and b = koy
first I need to check string length - is the are different - then write Error - it's easy
the I thought that I can compare strings in ASCII (case sensitivity is not important) and it' could be ok but...
I can create string tooky which have got the same size in ASCII but is not created from split and invert parts of first string...
any ideas?
static void Main(string[] args)
{
string S = "tokyo";
string T = "kyoto";
if (S.Length == T.Length)
{
split string ?
}
else
Console.WriteLine("This two words are different. No result found.");
Console.Read();
}
I would suggest doing the comparisons with strings. You can use the String.ToLower() method to convert them both to lowercase for comparison.
I am not exactly sure what problem you are trying to solve is, but from what I understand you are trying to check if string S can be split into two substrings that can be rearranged to make string T.
To check this you will want something similar to the following
for (int i = 0; i < S.length; i++) {
string back = S.substring(i);
string front = S.substring(0,i);
if (T.equals(back + front))
result = true;
}
Hope this helps
If you want to compare equality of two collections you should consider using LINQ:
static void Main(string[] args)
{
string S = "tokyo";
string T = "kyoto";
if (S.Length == T.Length)
{
if (S.Intersect(T).Any())
{
Console.WriteLine("The Contents are the same");
Console.Read();
}
}
else
Console.WriteLine("This two words are diferent. No result found.");
Console.Read();
}

Looking for best way to search and replace strings in another strings [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I'm writing a class that filters a lot of values. What's the best way to search/remove/replace a string in another string?
For example:
name +value (email)
The email, how to get it using? Linq? or .Split()? or Regular Expressions?
Which would have best performance?
Currently I'm using this:
string[] parts = val.Split('(');
string Email = parts[1].Replace(")", String.Empty);
On my machine, a variation of your code is the fastest (yours comes in second).
NOTE THE UNITS!! Ticks are 100 nanosecond increments.
SplitReplace takes 0.961795 ticks per call
Split takes 0.747009 ticks per call
Regex takes 2.512739 ticks per call
WithLinq takes 2.59299 ticks per call
My variation is just to only split (no replace):
string[] parts = val.Split('(', ')');
return parts[1];
The testing code...
[Test]
public void SO()
{
const string input = "name +value (email)";
TestGivenMethod(input, SplitReplace, "SplitReplace");
TestGivenMethod(input, JustSplit, "Split");
TestGivenMethod(input, WithRegex, "Regex");
TestGivenMethod(input, WithLinq, "WithLinq");
}
private void TestGivenMethod(string input, Func<string, string> method, string name)
{
Assert.AreEqual("email", method(input));
var sw = Stopwatch.StartNew();
string res = "";
for (int i = 0; i < 1000000; i++)
{
var email = method(input);
res = email;
}
sw.Stop();
Assert.AreEqual("email", res);
Console.WriteLine("{1} takes {0} ticks per call", sw.ElapsedTicks/1000000.0, name);
}
string SplitReplace(string val)
{
string[] parts = val.Split('(');
return parts[1].Replace(")", String.Empty);
}
string JustSplit(string val)
{
string[] parts = val.Split('(', ')');
return parts[1];
}
private static Regex method3Regex = new Regex(#"\(([\w#]+)\)");
string WithRegex(string val)
{
return method3Regex.Match(val).Groups[1].Value;
}
string WithLinq(string val)
{
return new string(val.SkipWhile(c => c != '(').Skip(1).TakeWhile(c => c != ')').ToArray());
}
I would recommend regular expression as I think it is invented for this reason which is search in a string and string replacement.
If I understand your question correctly, you're trying to replace the literal of (email) with an email likely provided from another source
var text = "name +value (email)";
var emailAddress = "someone#test.com";
text = Regex.Replace(text, #"\(email\)", emailAddress);
The code block above will replace '(email)' with the contents of the emailAddress variable
Be sure to add the appropriate using statement to the top of your code-file
using System.Text.RegularExpressions;
String.Split would be the most simplest and easy to understand approach as compared to Regular Expression and I am not sure How you can fit LINQ here.
As far as performance is concerned it would be best if you can do profiling against your test data to see actual performance difference between Regular Expression and String.Split

Categories