my program is intended to search for images url inside a string according to specific keywords. it actually works fine, only problem is the "search not found" error.
for some reason its like the code doesn't get to this "if" and wont return any error if there is no match found (last if).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Text.RegularExpressions;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
using (WebClient client = new WebClient())
{
int count = 0;
Regex SearchItem = new Regex("http://.+?\\.jpg");
string SearchValue = "fgdfgdf";
string htmlCode = "fsdflkjsdfkjsdfkjdsflkhttp://www.dssdtanya.jpgfsdf;ldsmfs;dlfms;dmfs";
Match matches = SearchItem.Match(htmlCode);
while (matches.Success)
{
string test = matches.ToString();
if (test.Contains(SearchValue))
{
count++;
Console.WriteLine("Result #{0}: '{1}' found in the source code at position {2}.",count, matches.Value, matches.Index);
matches = matches.NextMatch();
}
}
Console.WriteLine(count);
if (count == 0) { Console.WriteLine("search not found."); }
Console.ReadKey();
}
}
}
}
Your program enters an infinite loop if the first test doesn't contain the search value. Change your code to this:
while (matches.Success)
{
string test = matches.ToString();
if (test.Contains(SearchValue))
{
count++;
Console.WriteLine("Result #{0}: '{1}' found in the source code at position {2}.", count, matches.Value, matches.Index);
}
matches = matches.NextMatch(); //moved this outside the if
}
Related
i need an opc client for work, i used TitaniumAS as it's really simple, the read works fine but the write doesnt, i have the exception in the title
The tagID is correct as it works when i read it.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TitaniumAS.Opc.Client.Common;
using TitaniumAS.Opc.Client.Da;
using TitaniumAS.Opc.Client.Da.Browsing;
using System.Threading;
namespace OPCDA
{
class Program
{
static void Main(string[] args)
{
TitaniumAS.Opc.Client.Bootstrap.Initialize();
Uri url = UrlBuilder.Build("Kepware.KEPServerEX.V6");
using (var server = new OpcDaServer(url))
{
server.Connect();
//creating tag group
OpcDaGroup group = server.AddGroup("MyGroup");
group.IsActive= true;
//Write
OpcDaItem int2 = group.Items.FirstOrDefault(i => i.ItemId == "Channel1.Device1.Woord");
OpcDaItem[] items = { int2 };
object[] values = { 15601 };
HRESULT[] results = group.Write(items, values);
}
}
}
}
This code filters elements of certain category and finds and concatenates parameters although what needed is something a little more complex.
First of all, a person needs to be able to choose a category (out of a drop down list) or search and find the necessary ones.
And the second thing is that a user is supposed to specify what parameters he would like to combine (we have shared parameters txt fyi) and choose the order in which they are going to follow one another. Any resource on it or something similar to it would help greatly!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.UI.Selection;
namespace CombineParameters
{
[Transaction(TransactionMode.Manual)]
public class Class : IExternalCommand
{
public Result Execute(ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
//Application app = uiapp.Application;
Document doc = uidoc.Document;
//Create Filtered Element Collector and Filter
FilteredElementCollector collector = new FilteredElementCollector(doc);
ElementCategoryFilter filter = new ElementCategoryFilter(BuiltInCategory.OST_DuctFitting);
//Applying Filter
IList <Element> ducts = collector.WherePasses(filter).WhereElementIsNotElementType().ToElements();
foreach (Element e in ducts)
{
//Get Parameter values
string parameterValue1 = e.LookupParameter("AA").AsString();
string parameterValue2 = e.LookupParameter("BB").AsString();
string parameterValue3 = e.LookupParameter("CC").AsString();
string newValue = parameterValue1 + "-" + parameterValue2 + "-" + parameterValue3;
using (Transaction t = new Transaction(doc, "Set Parameter name"))
{
t.Start();
e.LookupParameter("DD").Set(newValue).ToString();
t.Commit();
}
}
return Result.Succeeded;
}
}
}
You want to combine user selected parameters in a specific order? Why dont you use a simple windows form gui.
Example
command.cs
#region Namespaces
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using System;
using System.Collections.Generic;
using System.Diagnostics;
#endregion
namespace combineParameters
{
[Transaction(TransactionMode.Manual)]
public class Command : IExternalCommand
{
public Result Execute(
ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Application app = uiapp.Application;
Document doc = uidoc.Document;
Form1 form = new Form1(doc);
//Show Dialouge form
form.ShowDialog();
return Result.Succeeded;
}
}
}
Forms1.cs
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace combineParameters
{
public partial class Form1 : System.Windows.Forms.Form
{
//Class variable
Document revitDoc { get; set; }
public Form1(Document doc)
{
InitializeComponent();
this.revitDoc = doc;
//Create a list of the parameters you want your user to choose from
List<string> stringParameters = new List<string>
{
"textParameter1",
"textParameter2",
"textParameter3",
"textParameter4"
};
//Add list to comboboxes on form
foreach (string parameterName in stringParameters)
{
comboBox1.Items.Insert(0, parameterName);
comboBox2.Items.Insert(0, parameterName);
comboBox3.Items.Insert(0, parameterName);
}
}
private void button1_Click(object sender, EventArgs e)
{
FilteredElementCollector collector = new FilteredElementCollector(revitDoc);
ElementCategoryFilter filter = new ElementCategoryFilter(BuiltInCategory.OST_DuctFitting);
//Applying Filter
IList<Element> ducts = collector.WherePasses(filter).WhereElementIsNotElementType().ToElements();
using (Transaction t = new Transaction(revitDoc, "Set Parameter name"))
{
//Use a try and catch for transactions
try
{
t.Start();
foreach (Element duct in ducts)
{
//Get Parameter values
string parameterValue1 = duct.LookupParameter(comboBox1.Text).AsString();
string parameterValue2 = duct.LookupParameter(comboBox2.Text).AsString();
string parameterValue3 = duct.LookupParameter(comboBox3.Text).AsString();
string newValue = parameterValue1 + "-" + parameterValue2 + "-" + parameterValue3;
//do not need .ToString() when setting parameter
duct.LookupParameter("NewParameter").Set(newValue);
}
t.Commit();
}
//Catch with error message
catch (Exception err)
{
TaskDialog.Show("Error", err.Message);
t.RollBack();
}
}
}
}
}
Snip of this example inside Revit:
Example photo
I am trying to create a program that is a character counter using
a linked list and I am struggling. I have to program this in C#. If anyone can see a solution please tell me. I am have troubling using a linked list with nodes trying to show the results of counting characters. What am I missing? What do I need to add? By the way I am using visual works to run my code.
(First file)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApp17
{
class CharacterFrequency
{
private char ch;
private int frequency;
public char Ch
{
get { return ch; }
set { ch = value; }
}
public int Frequency
{
get { return frequency; }
set { frequency = value; }
}
}
}
(Second file)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApp17
{
class Program
{
static void Main(string[] args)
{
int ch;
const int InputFile = 0;
const int OutputFile = 1;
StreamReader reader = null;
StreamWriter writer = null;
//Declaring Linked List
CharacterFrequency obj = new CharacterFrequency();
LinkedList list = new LinkedList<CharacterFrequency>();
//To read the characters in the Input File.
reader = new StreamReader(File.OpenRead(args[InputFile]));
//To insert text through the loop.
while ((ch = reader.Read()) != -1)
{
//Creating my linked list.
CharacterFrequency cf = LinkedList<CharacterFrequency>
list(ch);
//Creating my nodes.
LinkedListNode<CharacterFrequency> node;
node = list.Find(cf);
//Conditions
if (node != null)
{
node.Value.Add(cf, 1); //Increase in increments.
}
else
{
list.AddLast(cf); //If it is not on the list yet, add it.
}
}
reader.Close();
//Create my output file.
writer = new StreamWriter(File.OpenWrite(args[OutputFile]));
//Display on prompt.
foreach (CharacterFrequency cf in list)
writer.WriteLine(cf.ToString());
writer.Close();
}
}
}
How can I print by reading the names and surnames from the keyboard and making only the surname letters large?
string name;
Console.Write("Name and surname:");
name=Console.ReadLine();
int word= name.LastIndexOf(" ");
Console.WriteLine(name.Substring(word).ToUpper());
I could write on surname but I could not add name.Sorry my english..
I would use .Split() to separate first and last name
Console.Write("Name and surname:");
string name = Console.ReadLine();
Console.WriteLine(name.Split().First().ToUpper());
Console.WriteLine(name.Split().Last().ToUpper());
if you want to stay with Substring() this would be the approach
Console.Write("Name and surname:");
string name = Console.ReadLine();
int word = name.LastIndexOf(" ");
Console.WriteLine(name.Substring(word+1).ToUpper());
Console.WriteLine(name.Substring(0, word).ToUpper());
I solve my problem.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;/*Add this library for ToTitleCase*/
namespace String_denemeler
{
class Program
{
public static string ToTitleCase(string Text)/*And Add this for ToTitleCase*/
{
return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(Text);
}
static void Main(string[] args)
{
string name;
Console.Write("Name&Surname:");
name = Console.ReadLine();
name= ToTitleCase(name);
int index = name.LastIndexOf(" ");
Console.WriteLine(name.Substring(0, index) + name.Substring(index).ToUpper());
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace DiffCalc
{
public partial class newResult : Form
{
public newResult()
{
InitializeComponent();
}
private void newResult_Load(object sender, EventArgs e)
{
string s1 = "Have a very good";
string s2 = "Have a very good day, Joe";
IEnumerable<string> diff;
MatchCollection matches = Regex.Matches(s1, #"\b[\w']*\b");
IEnumerable<string> first = from m in matches.Cast<Match>()
where !string.IsNullOrEmpty(m.Value)
select TrimSuffix(m.Value);
MatchCollection matches1 = Regex.Matches(s2, #"\b[\w']*\b");
IEnumerable<string> second = from m in matches1.Cast<Match>()
where !string.IsNullOrEmpty(m.Value)
select TrimSuffix(m.Value);
if (second.Count() > first.Count())
{
diff = second.Except(first).ToList();
}
else
{
diff = first.Except(second).ToList();
}
}
static string TrimSuffix(string word)
{
int apostropheLocation = word.IndexOf('\'');
if (apostropheLocation != -1)
{
word = word.Substring(0, apostropheLocation);
}
}
}
}
Generating error
The call is ambiguous between the following methods or properties: 'System.Linq.Enumerable.Cast(System.Collections.IEnumerable)' and 'System.Linq.Enumerable.Cast(System.Collections.IEnumerable)'
Throwing this error at
matches.Cast()
line number 27 and similar instances. any help would be appericiated.