Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a string "\\server\printer" and I need to change it to `"printer on server:"
please note that "server and printer can vary in lengths.
var incomingText = #"\\server\printer";
var split = incomingText.Split(new[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
var decoratedText = split[1] + " on " + split[0];
String[] split = yourString.Split("\\");
Return split[1] + " on " + split[0];
Here you go
https://dotnetfiddle.net/ZsjrsN
var str = #"\\server\printer";
var matches = Regex.Match(str, #"[A-Za-z0-9]{1,}");
string str1 = matches.Value;
matches = matches.NextMatch();
string str2 = matches.Value;
string result = str2 + " on " + str1;
Related
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 5 years ago.
Improve this question
I am using this in my controller:
char[] arrDate = date.ToArray();
DateTime dt = DateTime.Parse(arrDate[0] + arrDate[1] + "/" +
arrDate[2] + arrDate[3] + "/" +
arrDate[4] + arrDate[5] + arrDate[6] + arrDate[7]);
The error:
System.FormatException: String was not recognized as a valid DateTime.
Consider this:
var date = "11252017";
var arrDate = date.ToArray();
var strDate = arrDate[0] + arrDate[1] + "/" +
arrDate[2] + arrDate[3] + "/" +
arrDate[4] + arrDate[5] + arrDate[6] + arrDate[7]; // 98/25/2017
Notice that:
'1' + '1' = 98* ⇒ char + char = int
98 + "/" = "98/" ⇒ int + string = string
"98/" + '2' = "98/2" ⇒ string + char = string
The fix:
var dt = DateTime.Parse("" +
arrDate[0] + arrDate[1] + "/" +
arrDate[2] + arrDate[3] + "/" +
arrDate[4] + arrDate[5] + arrDate[6] + arrDate[7]);
*ASCII representation:
'1' in decimal is 49
I assume date is of type string. For parsing a string the DateTime class has several methods of which ParseExact is one. This method can parse a string given a format specifier and a culture. In your case the date can be parsed like this:
var date = "11252017";
var dt = DateTime.ParseExact(date, "MMddyyyy", CultureInfo.InvariantCulture);
By the way a string is an array of chars, so in your code arrDate[0] is exactly the same as date[0]. Just something to keep in mind for the future.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Error "HtmlAgilityPack.HtmlNode.SelectSingleNode(...) return null."
My exemplary code.
string url = #"http://www.baza-firm.com.pl/?vm=zabrze&pg=2&b_szukaj=szukaj";
HtmlWeb web = new HtmlWeb();
//Process.Start(url); //wyswietlenie strony
var doc1 = web.Load(url);
MessageBox.Show(doc1.DocumentNode.OuterHtml.ToString());
var nazwa = doc1.DocumentNode.SelectSingleNode("//span[#class='przeppoz']").InnerText;
var ulica = doc1.DocumentNode.SelectSingleNode("//div[#itemprop='streetAddress']").InnerText;
var kod_pocztowy = doc1.DocumentNode.SelectSingleNode("//div/span[#itemprop='postalCode']").InnerText;
var miejscowość = doc1.DocumentNode.SelectSingleNode("//div/span[#itemprop='addressLocality']").InnerText;
var wojewodztwo = doc1.DocumentNode.SelectSingleNode("//div/span[#itemprop='addressRegion']").InnerText;
var telefon = doc1.DocumentNode.SelectNodes("//div[#class='divSMV_tel1 clearBoth']");
List<string> lista_tel = new List<string>();
foreach (var node in telefon)
{
lista_tel.Add(node.InnerText);
}
MessageBox.Show("nazwa " + nazwa
+ "\nkod pocztowy " + kod_pocztowy
+ "\nulica " + ulica
+ "\nkod pocztowy " + kod_pocztowy
+ "\nmiejscowość " + miejscowość
+ "\nwojewództwo " + wojewodztwo
+ "\ntelefon " + lista_tel[0].ToString());
Previously worked now shows a empty page. Why does not work Agility pack?
Your server returns 404 - Not Found as a response status, but also returns the html content you expect.
So, by changing the downloading code a little bit, your parsing code works...
string url = #"http://www.baza-firm.com.pl/?vm=zabrze&pg=2&b_szukaj=szukaj";
var doc1 = new HtmlAgilityPack.HtmlDocument();
using (var client = new HttpClient())
{
var resp = await client.GetAsync(url);
MessageBox.Show(resp.StatusCode.ToString());
var html = await resp.Content.ReadAsStringAsync();
doc1.LoadHtml(html);
}
MessageBox.Show(doc1.DocumentNode.OuterHtml.ToString());
.....
......
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
can someone please suggest a method wherein i can separate uppercase characters and lowercase characters in a string
input : "heLLoWorLd"
output : "heoordLLWL"
I have prepare one program for you to doing the same:
using System;
using System.Collections.Generic;
using System.IO;
class Program
{
static void Main()
{
// Input string.
string mixedCase = "heLLoWorLd";
// Call ToLower instance method, which returns a new copy.
string lower = "";
string uper = "";
for (int i = 0; i < mixedCase.Length; i++)
{
if (char.IsLower(mixedCase[i]))
lower = lower + mixedCase[i];
else
uper = uper + mixedCase[i];
}
// Display results.
Console.WriteLine("{0}{1}",
lower,
uper);
}
}
OUTPUT:
heoordLLWL
This code will definitely helpful to you.Thank you!
You can make use of few extension methods like the following:
string strInput="heLLoWorLd";
string outputStr =String.Join("",strInput.GroupBy(x=>Char.IsLower(x))
.SelectMany(y=>y.ToList()));
You can try a working example here
Why not just a simple OrderBy? Imo GroupBy and SelectMany is a bit cracking a nut with a sledgehammer
string input = "heLLoWorLd";
string output = string.Concat(input.OrderBy(char.IsUpper)); // heoordLLWL
Try this:
string input = "heLLoWorLd";
string output = string.Empty;
output = String.Concat(input.Where(c => Char.IsLower(c))) + String.Concat(input.Where(c => Char.IsUpper(c))) ;
Another way,
string str = "WeLcoMe";
string _upper = string.Empty, _lower = string.Empty;
foreach (var s in str)
{
if (char.IsUpper(s))
_upper += s;
else
_lower += s;
}
str = _upper + _lower;
Output will be
WLMecoe
string input = "heLLoWorLd";
StringBuilder builder = new StringBuilder();
StringBuilder upp = new StringBuilder();
StringBuilder low = new StringBuilder();
foreach (char c in input)
{
if (Char.IsLower(c))
{
low.Append(c);
}
else
{
if (Char.IsUpper(c))
{
upp.Append(c);
}
}
}
string output = low.ToString() + upp.ToString();
var input = "heLLoWorLd";
var output = string.Concat(input.GroupBy(char.IsLower).SelectMany(c => c.ToList()));
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 7 years ago.
Improve this question
I have the below code but I know it doesn't work as when I try declaring it, my code says it's not defined but I don't know how to write it.
I no I need to declare is before my IF statement but not sure how. Below is what I have but I know its wrong as I keep saying
if (Session["Step01Tel"] != "")
{
var ContactDetails = Step01TelLabel.Text + " " + Session["Step01Tel"].ToString();
}
else if (Session["Step01Email"] != "")
{
var ContactDetails = Step01EmailLabel.Text + " " + Session["Step01Email"].ToString();
}
Then I'm after something like the below in my code to call it as its for the body of my email that my site sends.
msg.Body = ContactDetails.Tostring()
The reason I'm after this is that if the Tel or Email field is empty then I don't want the Tel/Email label to be displayed in the email and you can not us an If inside an email body.
The below shows how I initially had it but as I said this displayed the field label with no value.
////NEED TO ONLY DISPLAY IF VALUE IS PRESENT
// Step01TelLabel.Text + " " + Session["Step01Tel"].ToString()
// + Environment.NewLine.ToString() +
// Step01EmailLabel.Text + " " + Session["Step01Email"].ToString()
// + Environment.NewLine.ToString() +
////
Try this
var ContactDetails = string.Empty;
if (Session["Step01Tel"] != "")
{
ContactDetails = Step01TelLabel.Text + " " + Session["Step01Tel"].ToString();
}
else if (Session["Step01Email"] != "")
{
ContactDetails = Step01EmailLabel.Text + " " + Session["Step01Email"].ToString();
}
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 am trying to read the File by Splitting each Row by removing whitespace through regex.split in c# but my code isn't working properly need help. Thanks in Advance.
text="";
OpenFileDialog open = new OpenFileDialog();
if (open.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Stream filestream = open.OpenFile();
if (filestream != null)
{
string filename = open.FileName;
text = File.ReadAllText(filename);
}
string code = textBox1.Text;
columncounter = code.Length;
string[] arrays = Regex.Split(code, #"\s+");
textBox1.Text = text;
}
If you are trying to write each row, without whitespace, to your TextBox, this should work:
var result = new StringBuilder();
foreach (var line in File.ReadAllLines(filename))
{
result.AppendLine(Regex.Replace(line, #"\s", "")));
}
textBox1.Text = result.ToString();
For faster performance, use string.Replace:
var result = new StringBuilder();
foreach (var line in File.ReadAllLines(filename))
{
result.AppendLine(line
.Replace("\t", "")
.Replace("\n", "")
.Replace("\r", "")
.Replace(" ", ""));
}
textBox1.Text = result.ToString();
Just add textBox1.text = string.Concat(arrays). It will concatenate the strings from the splitted array.
or you could do something like text.Replace(" ", "")