1

I am using following code:

var names = ConfigurationManager.AppSettings.AllKeys.Where(k => k.StartsWith("name"));

and i get keys like: name1, name2, name16, name18.

Now i want to create another array which will remove name and just keep 1,2,16,18. Is there any easy way to do this in above code itself? Or do it seperatly?

0

5 Answers 5

7

You can directly

ConfigurationManager.AppSettings.AllKeys.Where(k => k.StartsWith("name")).Select(a => a.Replace("name",""));
Sign up to request clarification or add additional context in comments.

2 Comments

Problems: There's no static Replace method, so it won't compile, I guess. It's bad to call ToList() before the Select (in the second code line).
It's a good solution, however it exploits a edge case of this question, if Name showed up more than once you would not get the same results, I think using SubString is a much better approach as you know your text starts with your search string from the thanks to the Where clause.
2

I think your code is good enough. Just a little bit of performance improve by using substring as it's straightforward operation to remove prefix:

var prefix = "name"; // could be a parameter or used as const; just for example
var nums = ConfigurationManager.AppSettings.AllKeys.Where(s => s.StartsWith(prefix)).Select(s => s.Substring(prefix.Length)).ToArray();

1 Comment

+1 very good solution. much better than the other suggestions, I would just say the ToArray() is not necessary and just leave it as a IEnumerable<string>
1

Try this:

var names = ConfigurationManager.AppSettings.AllKeys.Where(k => k.StartsWith("name")).Select(p => p.Replace("name", ""));

Comments

0

Use:

var namesWithoutPrefix = names.Select(n => n.Substring(4));

Doing Replace instead of Substring might replace several substrings in one name (and is slower even if it doesn't do so).

3 Comments

Liked this answer. But you think this is faster than the above mentioned code with Replace?
@NoviceMe Chances are with strings this short the performance difference between all of these methods will be so small you couldn't measure it. If the strings were a few million characters long then it might begin to matter.
@NoviceMe Yes, suppose (like Servy says) that n was ten million charaters long. To do Substring(4), you just have to copy the last 9'999'996 chars over to a new string instance. To do Replace("name", ""), you would have to check all positions (nearly ten million positions) to see if the four chars 'n','a','m','e' occur in this order, and only copy over to the new string when they don't. The latter will be slower.
0

I would not recommend relying on the position of the numeric value or the length of the string or the fact that the text reads 'name' at all. Instead you can use a simple regular expression to extract it consistently:

Regex regex = new Regex(@"[0-9]+");
var numbers = ConfigurationManager.AppSettings
    .AllKeys.Select(p => regex.Match(p).Value).ToArray();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.