首先:抱歉我的英语不好!
我知道标题不是最好的英文,但我真的不知道如何格式化这个问题...
我想要做的是逐行读取HTML源代码,这样当它看到一个给定的单词时(比如http://)它复制整个句子,所以我可以剥去其余的只保留URL.
这就是我尝试过的:
using (var source = new StreamReader(TempFile))
{
string line;
while ((line = source.ReadLine()) != null)
{
if (line.Contains("http://"))
{
Console.WriteLine(line);
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果我想从外部文件中读取它,但是当我想读取字符串或字符串构建器时它不起作用,那么这种方法是完美的,你如何逐行阅读?
我认为我误解了获得的目的; 组; 在C#中.我有一个我正在尝试填充的双打列表,我正在使用以下代码...这两个都在同一个类中,当我尝试运行它时,我在尝试填充列表时得到一个Null引用异常.我到底误解了什么?
public List<double> NewData
{ get; set; }
public InfoFile(String fileName, String groupName)
{
this.group = groupName;
test = File.ReadAllLines(fileName);
FileInfo label = new FileInfo(fileName);
this.name = Path.GetFileName(fileName);
isDrawn = false;
for (int t = 2; t < test.Length; t++)
{
NewData.Add(double.Parse(test[t].Substring(6, 4)));
}
}
Run Code Online (Sandbox Code Playgroud) 这是我的数组:
int myArray = new int[5];
myArray[0] = 1;
myArray[1] = 1;
myArray[2] = 1;
myArray[3] = 3;
myArray[4] = 5;
Run Code Online (Sandbox Code Playgroud)
如果我想让程序找到这个数组的模式,我该怎么写?
我想带一个包含很多行的文本文件并将其转换为数组.例如,如果文本文件是:
第1行
第2行
第3行
4号线
它会导致
string[] stringList = { "Line 1", "Line 2", "Line 3", "Line 4" };
Run Code Online (Sandbox Code Playgroud)
我该怎么做呢?
我试过这个:
string line;
string[] accountList;
using (StreamReader file = new StreamReader(accountFileLocation.Text))
{
while (line = file.ReadLine() != null)
{
stringList += line;
}
}
Run Code Online (Sandbox Code Playgroud)
然而,错误:
无法将类型'bool'隐式转换为'string'
无法将'string'类型转换为'string []'
无法将类型'string'隐式转换为'string []'
static void Main(string[] args)
{
string foo = "jason123x40";
char[] foo2 = foo.ToCharArray();
string foo3 = "";
for (int i = 0; i < foo2.Length; i++)
{
int num = 0;
Int32.TryParse(foo2[i].ToString(), out num);
if (num != 0)
{
foo3 += num.ToString();
}
}
Console.WriteLine(foo3);
Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)
所以假设我有一个名为"john10smith250"的字符串.结果应为"10250".但是我会用我的代码获得"125".
我过滤掉0的原因是因为我不希望任何非数字字符被视为零.
有没有更好的方法将字符串的一部分转换为int?
string[] filesHD = Directory.GetFiles(dirPath, filepattern1);
string[] filesDT = Directory.GetFiles(dirPath, filepattern2);
string[] filesTD = Directory.GetFiles(dirPath, filepattern3);
Run Code Online (Sandbox Code Playgroud)
我的filesHD[]数组包含2个文件.filesDT[]包含2个文件,filesTD[]还包含2个文件.
我想创建一个字符串数组,它包含所有的6个文件filesHD,filesDT,filesTD.
string[] Allfiles = new string [filesHD + filesDT + filesTD]
Run Code Online (Sandbox Code Playgroud) 问题是该文件未保存为JPEG.只是一个普通的文件.
到目前为止这是我的代码:
private void btnSave_Click(object sender, EventArgs e)
{
saveDialog.FileName = txtModelName.Text;
if (saveDialog.ShowDialog() == DialogResult.OK)
{
Bitmap bmp = new Bitmap(pnlDraw.Width, pnlDraw.Height);
pnlDraw.DrawToBitmap(bmp, new Rectangle(0, 0,
pnlDraw.Width, pnlDraw.Height));
bmp.Save(saveDialog.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
Run Code Online (Sandbox Code Playgroud) public int dialog()
{
Form prompt = new Form(); // creates form
//dimensions
prompt.Width = 300;
prompt.Height = 125;
prompt.Text = "Adding Rows"; // title
Label amountLabel = new Label() { Left = 50, Top = 0, Text = "Enter a number from 1-50" }; // label for prompt
amountLabel.Font = new Font("Microsoft Sans Serif", 9.75F);
TextBox value = new TextBox() { Left = 50, Top = 25, Width = prompt.Width / 2 }; // text box for prompt
Button …Run Code Online (Sandbox Code Playgroud) 在具有列表(最多3个)的控制器中,与密码检查相关的错误消息将存储在名为Password的属性中.
IEnumerable<PasswordMessages> passwordMessage = LoanTrackerServices.CheckPasswordRequirements(model.NewPassword, model.EmailId);
if ( passwordMessage.Count() > 0 )
{
foreach (PasswordMessages pm in passwordMessage)
{
ModelState.AddModelError("Password",( pm.Message));
}
LoginPageModel loginModel = new LoginPageModel();
return View("Index", new HomePageModel() { Register = model, Login = loginModel });
}
Run Code Online (Sandbox Code Playgroud)
但在我看来,我无法弄清楚如何获取所有这些(最多3个)错误消息.现在只显示列表中的第一条消息.这是我的代码
for (int i = 0; i < ViewData.ModelState["Password"].Errors.Count; i++)
{
@Html.ValidationMessage("Password")
}
Run Code Online (Sandbox Code Playgroud)
如何获取密码中存储的所有错误消息?
我有一个双数组 Double[] array = new Double[5];
例如,如果数组包含如下数据:
{0.5,1.5,1.1,0.6,2}
如何找到最接近1的数字?输出应该是1.1,因为在这种情况下它是最接近1的那个.
c# ×10
arrays ×3
.net ×2
asp.net-mvc ×1
closest ×1
int ×1
label ×1
list ×1
mode ×1
streamreader ×1
string ×1
text-files ×1
winforms ×1