Replace is adding to end of string - c#

I am trying to do a simple replace. My problem is that instead of replacing the string, it appears it grabs the last couple characters and appends them to the end.
public static Dictionary<string, string> dict = new Dictionary<string, string> {
{"1", #"\\id 1ch\n fjdsbfjdshfd"},
{"2", #"\\id 1co\n fjdsbfjdshfd"},
{"3", #"\\id 1th\n fjdsbfjdshfd"}
};
public static string bookNameFn(string line)
{
return line.Split(' ')[1].ToLower().Replace("1ch", "1Chr").Replace("1co", "1Cor").Replace("1th", "1Thess");
}
foreach (KeyValuePair<String, String> usfm in dict)
{
foreach (var line in usfm.Value.Split('\n'))
{
if (line.StartsWith("\\id "))
{
Console.WriteLine(bookNameFn(line));
}
}
}
output
1Chrr
1Corr
1Thessess
desired output
1Chr
1Cor
1Thess

Use literal notation e.g.
#"\id 1th\n fjdsbfjdshfd";
It will work, the problem is the way the value is interpreted through the string constructor.

Related

Read ini sections with no values and append it to a dictionary

I have the following ini file with sections and keys but no values asigned:
[core]
bul_gravel_heli
ent_dst_concrete_large
bul_wood_splinter
[cut_armenian1]
cs_arm2_muz_smg
cs_ped_foot_dusty
What I want to do is:
Read all the secions and vaues.
Store them in a dictionary in the format:
{section: {key1, key2, key3, key4, etc}
Now the problem is that I can't find anywhere an example of ini file reading without values, all the results I've found are for reading ini files without sections.
To give a brief of what I want to do with the stored dictionary is this:
There's a function public void AddList(string listName, List<dynamic> list) and for each one of the dictionary keys and values I want to create the method. I already know that I can use for loops but I'm stuck at parsing the ini file.
Well, a simple foreach loop should do:
private static Dictionary<string, List<string>> IniToDictionary(IEnumerable<string> lines) {
Dictionary<string, List<string>> result =
new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
string category = "";
foreach (string line in lines) {
string record = line.Trim();
if (string.IsNullOrEmpty(record) || record.StartsWith("#"))
continue;
else if (record.StartsWith("[") && record.EndsWith("]"))
category = record.Substring(1, record.Length - 2);
else {
int index = record.IndexOf('=');
string name = index > 0 ? record.Substring(0, index) : record;
if (result.TryGetValue(category, out List<string> list))
list.Add(name);
else
result.Add(category, new List<string>() { name});
}
}
return result;
}
If you want to process a file:
Dictionary<string, List<string> result = IniToDictionary(File
.ReadLines(#"c:\MyIniFile.ini"));
Let's have a look (at test input):
Console.Write(tring.Join(Environment.NewLine, result
.Select(pair => $"{pair.Key,-15} : [{string.Join(", ", pair.Value)}]")));
Outcome:
core : [bul_gravel_heli, ent_dst_concrete_large, bul_wood_splinter]
cut_armenian1 : [cs_arm2_muz_smg, cs_ped_foot_dusty]

Finding values in a string:string dictionary where a given filter string is a subset of the key string

I have a Dictionary<string, string> object where values are stored that look like this:
examplePlanet : defaultText0
examplePlanet* : defaultText1
examplePlanet** : defaultText2
examplePlanetSpecificlocationA : specificAText0
examplePlanetSpecificlocationA* : specificAText1
examplePlanetSpecificlocationB : specificBText
And I have a string filter that matches one of these keys or is a subset of a key.
This filter has the form planetLocation, which can be split into planet and location.
My goal is to create a list of values where the filter is matched in this way: if planetLocation exists in the dictionary, add its value, and all values where the key matches but has extra *'s, to a list.
If planetLocation does not exist, only add values where the key matches the planet part of the filter (with possible extra *'s).
Basically, I want all values where the filter matches the key as much as possible.
Examples:
examplePlanetSpecificlocationA gives [specificAText0, specificAText1]
examplePlanetSpecificlocationB gives [specificBText]
examplePlanetSpecificlocationC gives [defaultText0, defaultText1, defaultText2]
I have already tried (among other things that didn't work) this:
private List<string> filteredResults;
///<summary>Filters dictionaries and returns a list of values</summary>
private List<string> GetFilteredResults(Dictionary<string, string> inputdictionary, string filter)
{
List<string> _filteredResults = new List<string>();
foreach (KeyValuePair<string, string> entry in inputdictionary)
{
if (entry.Key.Contains(filter))
{
_filteredResults.Add(entry.Value);
}
}
return _filteredResults;
}
public void main()
{
//stuff happens here that assigns a value to filterPlanet and filterLocation
filteredResults = new List<string>();
filteredResults = GetFilteredResults(exampledictionary, filterPlanet + filterLocation);
if (filteredResults.Count == 0)
{
filteredResults = GetFilteredResults(exampledictionary, filterPlanet);
}
//do stuff with the filtered results
}
This almost worked, but returns all values where the key contains filterPlanet and not just filterPlanet itself plus possible *'s. I'm not sure how to make this function do what I want, and even if it somehow works I'm sure there is a more efficient way of filtering than this. Could you please help me here?
I wouldn't use the *'s as a way of differentiating multiple values for the same key, and I'd keep filterPlanet and filterLocation separate. That way you can use a simple O(1) dictionary lookup, rather than iterating across all keys, doing substring searching, etc.
public class PlanetFilterer
{
private readonly Dictionary<string, List<string>> lookup = new Dictionary<string, List<string>>();
public PlanetFilterer(IEnumerable<(string filter, string value)> filters)
{
foreach (var (filter, value) in filters)
{
var filterWithoutStars = filter.TrimEnd('*');
if (!lookup.TryGetValue(filterWithoutStars, out var values))
{
values = new List<string>();
lookup[filterWithoutStars] = values;
}
values.Add(value);
}
}
public IReadOnlyList<string> Lookup(string planet, string location)
{
List<string> results;
if (lookup.TryGetValue(planet + location, out results))
{
return results;
}
if (lookup.TryGetValue(planet, out results))
{
return results;
}
return Array.Empty<string>();
}
}
Usage:
var filters = new[]
{
("examplePlanet", "defaultText0"),
("examplePlanet*", "defaultText1"),
("examplePlanet**", "defaultText2"),
("examplePlanetSpecificlocationA", "specificAText0"),
("examplePlanetSpecificlocationA*", "specificAText1"),
("examplePlanetSpecificlocationB", "specificBText"),
};
var filterer = new PlanetFilterer(filters);
Console.WriteLine(string.Join(", ", filterer.Lookup("examplePlanet", "SpecificlocationA")));
Console.WriteLine(string.Join(", ", filterer.Lookup("examplePlanet", "SpecificlocationB")));
Console.WriteLine(string.Join(", ", filterer.Lookup("examplePlanet", "SpecificlocationC")));
Try it online

RegEx not being able to match data

i am using RegEx to find and replace data.
suppose my code is
Dictionary<string, string> data=new Dictionary<string, string>();
data.Add("JID","421587");
string template ="Your unique job number is {JID} .Your part will be collected by us"
string result = Regex.Replace(template, #"{([^{}]+)}", delegate(Match match)
{
string key = match.Groups[1].Value;
return data[key];
});
_body = result;
return this;
actually i am trying to find out this character {JID} and want to replace {JID} with the value which is store in dictionary but i guess the patter i used here #"{([^{}]+)} is not right to find out the data and replace it with {JID} .
so please help me to how to find {JID} from string and replace. thanks
EDIT
actually the text i was trying to replace that was huge html text and may that is why problem occur.
so now i manage the whole thing this way.
public MailTemplate ParseTemplate(Dictionary<string, string> data)
{
string template = GetBody();
foreach(KeyValuePair<string, string> item in data)
{
if (item.Key.ToUpper() != "ATTACHEDFILEPATH")
{
template = template.Replace("{" + item.Key + "}", item.Value);
}
}
_body = template;
return this;
}

String parsing (splitting and retrieving parameters)

I have a string formatted this way:
<?TAG param1="val1" parm2="val2" paramN="valN" /><?TAG param1="val1" parm2="val2" paramN="valN"/><?TAG param1="val1" parm2="val2" paramN="valN"/>
"TAG" is always the same value, but number of occurrences is variable and the number of parameters for each occurrence too. I can't change the source format.
I need to get the list of parameters for each occurrence using C# (.NET 4.0) Can you help me out?
XElement rootElement = XElement.Parse(string.Format("<element>{0}</element>",
yourString.Replace("?TAG", "TAG")));
var elements = rootElement.Elements();
var yourResult = elements.Select(x => new TagsAndParams { Tag = x,
Params = x.Attributes.Where(xa => xa.Name.LocalName.BeginsWith("param") });
With this class as a result holder (I know I could use anonymous types, but this is better for passing to other functions):
public class TagsAndParams
{
XElement Tag;
IEnumerable<XAttribute> Params;
}
You could do it with a nasty looking RegEx, but I'd make sure it's not actually an XML PI chain first:
(?<tag><?TAG (?<parm>param\d{1,2}=\"[^\"]+\"\s*)*\/\>)*
This will match groups, each group containing:
full tag
paramX="valX" pair
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
class ExampleClass
{
static void Main(string[] args)
{
string example = "<?TAG param1=\"val1\" param2=\"val2\" paramN=\"valN\" /><?TAG param1=\"val1\" param2=\"val2\" paramN=\"valN\"/><?TAG param1=\"val1\" param2=\"val2\" paramN=\"valN\"/>";
List<Dictionary<string, string>> result = new List<Dictionary<string, string>>();
string[] tokens = Regex.Split(example, "/><\\?TAG|<\\?TAG|/>");
foreach (string token in tokens) if (token.Length > 0)
{
Dictionary<string, string> parameters = new Dictionary<string, string>();
string[] parms = token.Split(' ');
foreach (string parm in parms) if (parm.Length > 0)
{
string[] keyvalue = Regex.Split(parm, "=\"|\"");
parameters.Add(keyvalue[0], keyvalue[1]);
}
result.Add(parameters);
}
Console.WriteLine("TAGs detected: " + result.Count);
foreach (Dictionary<string, string> token in result)
{
Console.WriteLine("TAG");
foreach (KeyValuePair<string, string> kvp in token)
Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
}
}
}
I've finally solved using this code (provided by a friend of mine). The trick was the Regex used for splitting individual elements. Thank you for the support, I will make use of the tip about xml parser in future :)

How can you change a ";" seperated string to some kind of dictionary?

I have a string like this:
"user=u123;name=Test;lastname=User"
I want to get a dictionary for this string like this:
user "u123"
name "Test"
lastname "User"
this way I can easely access the data within the string.
I want to do this in C#.
EDIT:
This is what I have so far:
public static Dictionary<string, string> ValueToDictionary(string value)
{
Dictionary<string, string> result = null;
result = new Dictionary<string, string>();
string[] values = value.Split(';');
foreach (string val in values)
{
string[] valueParts = val.Split('=');
result.Add(valueParts[0], valueParts[1]);
}
return result;
}
But to be honest I really think there is a better way to do this.
Cheers,
M.
You can use LINQ:
var text = "user=u123;name=Test;lastname=User";
var dictionary = (from t in text.Split( ";".ToCharArray() )
let pair = t.Split( "=".ToCharArray(), 2 )
select pair).ToDictionary( p => p[0], p => p[1] );
Split the string by ";".
Iterate over every element in the resulting array and split every element by "=".
Now;
dictionary.add(element[0], element[1]);
I Hope I made it clear enough.
Dictionary<string, string> d = new Dictionary<string, string>();
string s1 = "user=u123;name=Test;lastname=User";
foreach (string s2 in s1.Split(';'))
{
string[] split = s2.Split('=');
d.Add(split[0], split[1]);
}
var dictionary = new Dictionary<string, string>();
var linedValue = "user=u123;name=Test;lastname=User";
var kvps = linedValue.Split(new[] { ';' }); // you may use StringSplitOptions.RemoveEmptyEntries
foreach (var kvp in kvps)
{
var kvpSplit = kvp.Split(new[] { '=' });
var key = kvpSplit.ElementAtOrDefault(0);
var value = kvpSplit.ElementAtOrDefault(1);
dictionary.Add(key, value);
// you may check with .ContainsKey if key is already persistant
// you may check if key and value with string.IsNullOrEmpty
}
If you know for sure that there are no separator chars in your input data, the following works
string input = "user=u123;name=Test;lastname=User";
string[] fragments = input.Split(";=".ToArray());
Dictionary<string,string> result = new Dictionary<string,string>()
for(int i=0; i<fragments.Length-1;i+=2)
result.Add(fragments[i],fragments[i+1]);
It might perform slightly better than some of the other solutions, since it only calls Split() once. Usually I would go for any of the other solutions here, especially if readability of the code is of any value to you.
I think I would do it like this...
String s = "user=u123;name=Test;lastname=User";
Dictionary<string,string> dict = s.ToDictionary();
The implementation of ToDictonary is the same as yours except that I would implement it as an extension method. It does look more natural.

Categories