Replace symbols in file c# - c#

Hey, I'm trying to replace some symbols in file. I made dictionary
and I can replace it in my string that I input. How to read my file,
do my replace and save it to another?
class Program
{
static void Main(string[] args)
{
Translit translit = new Translit();
StreamReader sr = new StreamReader("test.txt");
string testIn = "iconb "; //a test input string
string testOut = translit.TranslitFileName(testIn);
Console.WriteLine("Inputed \'{0}\'", testIn);
Console.WriteLine("after \'{0}\'", testOut);
Console.ReadLine();
}
public class Translit
{
Dictionary<string, string> dictionaryChar = new Dictionary<string, string>()
{
{"а","a"},
{"е","e"},
{"о","o"},
{"р","p"},
{"с","c"}
};
public string TranslitFileName(string source)
{
var result = "";
//symbols for replace
foreach (var ch in source)
{
var ss = "";
//compare dictionary keys
if (dictionaryChar.TryGetValue(ch.ToString(), out ss))
{
result += ss;
}
else result += ch;
}
return result;
}
}
}

Try doing it this way:
Func<string, string> map = new []
{
new { input = 'a', output = 'x' },
new { input = 'e', output = 'x' },
new { input = 'o', output = 'x' },
new { input = 'p', output = 'x' },
new { input = 'c', output = 'x' },
}
.Select(x => (Func<string, string>)(s => s.Replace(x.input, x.output)))
.Aggregate((f0, f1) => x => f1(f0(x)));
File.WriteAllText("output.text", map(File.ReadAllText("test.txt")));
Calling map("Hello") produces "Hxllx" given my map code above.

Related

Inserting data to .CSV file at the same time using foreach

I am new here and actually very new to c#.
In a nutshell, I am using c# via Visual Studio, I am calling a data from a database and I want to save these data in a .csv file. The problem now is that I want to save these data on two columns at the same time.
My code do write them in a file but shifted not on the right rows.
Dictionary<string, string> elementNames = new Dictionary<string, string>();
Dictionary<string, string> elementTypes = new Dictionary<string, string>();
var nodes = webservice.nepService.GetAllElementsOfElementType(webservice.ext, "Busbar", ref elementNames, ref elementTypes);
Dictionary<string, string> nodeResults = new Dictionary<string, string>();
Dictionary<string, string> nodeResults1 = new Dictionary<string, string>();
foreach (var nodename in elementNames.Values)
{
var nodeRes = webservice.nepService.GetResultElementByName(webservice.ext, nodename, "Busbar", -1, "LoadFlow", null);
var Uvolt = GetXMLAttribute(nodeRes, "U");
nodeResults.Add(nodename, Uvolt);
var Upercentage = GetXMLAttribute(nodeRes, "Up");
nodeResults1.Add(nodename, Upercentage);
StringBuilder strBldr = new StringBuilder();
string outputFile = #"C:\Users\12.csv";
string separator = ",";
foreach (var res in nodeResults)
{
strBldr.AppendLine($"{res.Key}{separator}{res.Value}");
}
foreach (var res1 in nodeResults1)
{
strBldr.AppendLine($"{separator}{separator}{res1.Value}");
}
File.WriteAllText(outputFile, strBldr.ToString());
}
this is the output of the previous code:
https://ibb.co/T4trQC3
I want these shifted values to move up beside the other values like that:
https://ibb.co/4S25v0h
Thank you
if you look to the code you are using AppendLine
strBldr.AppendLine($"{separator}{separator}{res1.Value}");
and if you want to append on same line just use Append
strBldr.Append($"{separator}{separator}{res1.Value}");
EDITED:
in linq you can use Zip function to zip to lists
// using System.Linq;
var results = Results.Zip(Results1, (firstList, secondList) => firstList.Key + "," + firstList.Value + "," + secondList.Value);
Edit Full example
public static IDictionary<string, string> Results { get; set; }
public static IDictionary<string, string> Results1 { get; set; }
private static void Main(string[] args)
{
StringBuilder strBldr = new StringBuilder();
string outputFile = #"D:\12.csv";
Results = new Dictionary<string, string>()
{
{"N1", "20"},
{"N2", "0.399992"},
{"N3", "0.369442"},
{"N4", "0.369976"}
};
Results1 = new Dictionary<string, string>()
{
{"N1", "100"},
{"N2", "99.9805"},
{"N3", "92.36053"},
{"N4", "92.49407"}
};
IEnumerable<string> results = Results.Zip(Results1,
(firstList, secondList) => firstList.Key + "," + firstList.Value + "," + secondList.Value);
foreach (string res1 in results)
{
strBldr.AppendLine(res1);
}
File.WriteAllText(outputFile, strBldr.ToString());
}
for faster code you can try this
HashSet<Tuple<string, string, string>> values = new HashSet<Tuple<string, string, string>>();
var nodes = webservice.nepService.GetAllElementsOfElementType(webservice.ext, "Busbar", ref elementNames, ref elementTypes);
foreach (var nodename in elementNames.Values)
{
var nodeRes = webservice.nepService.GetResultElementByName(webservice.ext, nodename, "Busbar", -1, "LoadFlow", null);
var Uvolt = GetXMLAttribute(nodeRes, "U");
var Upercentage = GetXMLAttribute(nodeRes, "Up");
values.Add(Tuple.Create(nodename, Uvolt, Upercentage));
}
var output = string.Join("\n", values.ToList().Select(tuple => $"{tuple.Item1},{tuple.Item2},{tuple.Item3}").ToList());
string outputFile = #"C:\Users\12.csv";
File.WriteAllText(outputFile, output);
if the rowCount for Results and Results1 are same and the keys are in the same order, try:
for (int i = 0; i < Results.Count; i++)
strBldr.AppendLine($"{Results[i].Key}{separator}{Results[i].Value}{separator}{Results1[i].Value}");
Or, if the rows are not in the same order, try:
foreach (var res in Results)
strBldr.AppendLine($"{res.Key}{separator}{res.Value}{separator}{Results1.Single(x => x.Key == res.Key).Value}");

ambiguity in String startswith the given string

I need to see if string starts with a given string but I am getting ambiguity, here is my code:
string input = "balance1234";
string[] arr = new string[]
{
"bal",
"balance",
};
foreach (string s in arr)
{
if (input.StartsWith(s))
{
var rq= input.Replace(s, "");
}
}
If input is balance1234 , the if condition has to satisfy only with balance, but in my code it is satisfying with bal first.
Here is the solution (using the Hint given by Mr. Skeet):
string input = "balance1234";
string[] arr = new string[]
{
"bal",
"balance",
};
string rq = input;
foreach (string s in arr.OrderByDescending(x => x.Length))
{
if (input.StartsWith(s))
{
rq = input.Replace(s, "");
break;
}
}

C# Trying to split a string to get json object value

I am trying to split a string to get json object value - I have text values with numerous lines in the format:
new Car() { Id = 1, Year = 1926, Make = "Chrysler", Model = "Imperial", ImageUrl = "{"data":{"images":[{"thumb_url":"https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcRPe4CygIW-MuZL5jl77wlgXXK5_ANyC9l1X4QqLizCOkaVAlRe","image_url":"http://imperialclub.org/Yr/1926/photos/Phaeton2Big.jpg","width":1632,"height":1032}]},"error_code":0,"error":false,"message":"1 images(s) available"}" },
new Car() { Id = 2, Year = 1950, Make = "Hillman", Model = "Minx Magnificent", ImageUrl = "{"data":{"images":[{"thumb_url":"https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcScVsGEeRBh6xZYXr6Gm35Sk5ecSlk_ax3qZmoGRAtBbZC8vJZ9","image_url":"http://i.ebayimg.com/images/g/gcIAAOSwKadXPeLs/s-l300.jpg","width":300,"height":225}]},"error_code":0,"error":false,"message":"1 images(s) available"}" },
new Car() { Id = 3, Year = 1954, Make = "Chevrolet", Model = "Corvette", ImageUrl = "{"data":{"images":[{"thumb_url":"https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcSdZntu4tgWrZrxwqeuKlteCP9vJGnqUlmNq5JF1bBCf-EJy5r8","image_url":"http://momentcar.com/images/chevrolet-corvette-1954-1.jpg","width":1000,"height":600}]},"error_code":0,"error":false,"message":"1 images(s) available"}" },
What I would really like is to get them in the format:
new Car() { Id = 1, Year = 1926, Make = "Chrysler", Model = "Imperial", ImageUrl = "https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcRPe4CygIW-MuZL5jl77wlgXXK5_ANyC9l1X4QqLizCOkaVAlRe" },
new Car() { Id = 2, Year = 1950, Make = "Hillman", Model = "Minx Magnificent", ImageUrl = "https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcScVsGEeRBh6xZYXr6Gm35Sk5ecSlk_ax3qZmoGRAtBbZC8vJZ9" },
new Car() { Id = 3, Year = 1954, Make = "Chevrolet", Model = "Corvette", ImageUrl = "https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcSdZntu4tgWrZrxwqeuKlteCP9vJGnqUlmNq5JF1bBCf-EJy5r8" },
I know I can use JObject.Parse(data); to parse the json value - but just tring to get to it is becoming a bit of a nightmare. Is there a better way of doing this?
What I have so far:
static void Main(string[] args)
{
using (StreamWriter writer = new StreamWriter(#"c:\Data\temp\output.txt")) // file to write to
{
using (StreamReader reader = new StreamReader(#"c:\Data\temp\test.txt")) //file to read from
{
string line;
while (reader.ReadLine() != null)
{
line = reader.ReadLine();
string[] words = JsonSplitString(line);
string json = words[1];
writer.WriteLine("{0}", json);
}
}
}
}
static string[] JsonSplitString(string data)
{
return data.Split(new string[] { "ImageUrl" }, StringSplitOptions.None);
}
However I am getting a NullReferenceException - even though a string is being passed in to the JsonSplitString method.
You are calling reader.Readline() twice: once for the comparison and then again inside your loop. You are actually skipping every other line. And what is probably happening is that you are reaching the end of your file and then calling reader.Readline() again, which is null. Try this instead:
line = reader.ReadLine();
while (line != null)
{
string[] words = JsonSplitString(line);
string json = words[1];
writer.WriteLine("{0}", json);
line = reader.ReadLine();
}
using System;
using Newtonsoft.Json.Linq;
namespace JsonExperiments
{
class Program
{
static void Main(string[] args)
{
ExecuteEmployeeSearch();
Console.ReadLine();
}
static void ExecuteEmployeeSearch()
{
// mockup JSON that would be returned from API
string sampleJson = "{\"results\":[" +
"{\"employeename\":\"name1\",\"employeesupervisor\":\"supervisor1\"}," +
"{\"employeename\":\"name2\",\"employeesupervisor\":\"supervisor1\"}," +
"{\"employeename\":\"name3\",\"employeesupervisor\":[\"supervisor1\",\"supervisor2\"]}" +
"]}";
// Parse JSON into dynamic object, convenient!
JObject results = JObject.Parse(sampleJson);
// Process each employee
foreach (var result in results["results"])
{
// this can be a string or null
string employeeName = (string)result["employeename"];
// this can be a string or array, how can we tell which it is
JToken supervisor = result["employeesupervisor"];
string supervisorName = "";
if (supervisor is JValue)
{
supervisorName = (string)supervisor;
}
else if (supervisor is JArray)
{
// can pick one, or flatten array to a string
supervisorName = (string)((JArray)supervisor).First;
}
Console.WriteLine("Employee: {0}, Supervisor: {1}", employeeName, supervisorName);
}
}
}
}

foreach over anonymous types

using System;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
namespace LearningJustCode
{
class Program
{
static void Main(string[] args)
{
Update();
}
static void Update()
{
var quote1 = new { Stock = "DELL", Quote = GetQuote("DELL") };
var quote2 = new { Stock = "MSFT", Quote = GetQuote("MSFT") };
var quote3 = new { Stock = "GOOG", Quote = GetQuote("GOOG") };
var quotes = new object[] { quote1, quote2, quote3 };
foreach (var item in quotes)
{
Console.WriteLine(item.Stock);
Console.WriteLine(item.Quote.ToString());
}
Console.ReadKey();
}
static string GetQuote(string stock)
{
try
{
return InnerGetQuote(stock);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return "N/A";
}
}
static string InnerGetQuote(string stock)
{
string url = #"http://www.webservicex.net/stockquote.asmx/GetQuote?symbol={0}";
var request = HttpWebRequest.Create(string.Format(url, stock));
using (var response = request.GetResponse())
{
using (var reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII))
{
return reader.ReadToEnd();
}
}
}
}
}
I am getting an squiggley over item;Variable 'item' is only assigned to. This code has been slightly modified from Paul Kimmel's book C# Unleashed by Sams.
Your array needs to be of the type of your stock quote.
Your stock quote is an anonymous type, so we need to define the array anonymously as well. This can be done cleanly as:
var quotes = new[]{ new { Stock = "DELL", Quote = "123" },
new { Stock = "MSFT", Quote = "123" },
new { Stock = "GOOG", Quote = "123" }};
foreach (var item in quotes)
{
Console.WriteLine(item.Stock);
Console.WriteLine(item.Quote.ToString());
}
i guess the problem is in that line:
var quotes = new object[] { quote1, quote2, quote3 };
quotes is a object array, not an array of that anonymous type. the foreach also has just the object value. you could try to form the array within one line or within a lambda expression
A very dirty workaround is changing 'var' to 'dynamic'
var quote1 = new { Stock = "DELL", Quote = ("DELL") };
var quote2 = new { Stock = "MSFT", Quote = ("MSFT") };
var quote3 = new { Stock = "GOOG", Quote = ("GOOG") };
var quotes = new object[] { quote1, quote2, quote3 };
foreach (dynamic item in quotes)
{
var r = item.Stock;
}
a cleaner solution is leaving out 'object', so the compiler can generate an anonymous typed array
var quote1 = new { Stock = "DELL", Quote = ("DELL") };
var quote2 = new { Stock = "MSFT", Quote = ("MSFT") };
var quote3 = new { Stock = "GOOG", Quote = ("GOOG") };
var quotes = new [] { quote1, quote2, quote3 };
foreach (var item in quotes)
{
var r = item.Stock;
}

NVelocity merge fails if last character in template is $

I've got a strange problem using the Castle NVelocity...
If the template string ends with a $ character, it throws the following and exception:
Lexical error: NVelocity.Runtime.Parser.TokenMgrError: Lexical error
at line 1, column 94. Encountered: after : ""
If i add a space, or any other character, to the end of the string it works as expected.
Does anybody have a workaround for this?
Here's some sample code that reproduces the problem:
class Program
{
static void Main(string[] args)
{
var result = Merge("The quick brown ${Animal} jumps over the lazy dog$", new List<KeyValuePair<string, string>>() { new KeyValuePair<string, string>("Animal", "fox") });
Console.WriteLine("Success: {0}", result.Key);
Console.WriteLine("Message: {0}", result.Value);
Console.Read();
}
public static KeyValuePair<bool, string> Merge(string template, IList<KeyValuePair<string, string>> data)
{
var ret = new KeyValuePair<bool, string>(false, null);
try
{
if (data != null)
{
var engine = new VelocityEngine();
engine.Init();
var context = new VelocityContext();
foreach (var tokenData in data)
{
context.Put(tokenData.Key, tokenData.Value);
}
var templateContent = template;
var sb = new StringBuilder();
var textWriter = new StringWriter(sb);
engine.Evaluate(context, textWriter, String.Empty, templateContent);
ret = new KeyValuePair<bool, string>(true, sb.ToString());
}
}
catch (Exception ex)
{
ret = new KeyValuePair<bool, string>(false, ex.Message);
}
return ret;
}
}
You have a couple of options:
If you have no influence over the input string to be merged, make sure that they don't have a trailing dollar character, at least not for the merge process:
Example:
bool dollarAtEnd = false;
if (input.EndsWith('$'))
{
input += " ";
dollarAtEnd = true;
}
var result = Merge(input, ...);
if (dollarAtEnd)
{
result = result.Substring(1, result.Length - 1);
}
If you can control the input string, but only have the requirement that some of them should end with a dollar character, you can do as follows:
Example:
"#set($dollar='$')The quick brown ${Animal} jumps over the lazy dog$dollar"
Or pass "dollar" as variable to the VelocityContext, rather than specifying it inline.
$ denotes the start of a variable name, if you want to use a $ literal, you need to escape it to \$.
http://velocity.apache.org/engine/devel/user-guide.html#Getting_literal

Categories