我的代碼無法正常作業。您應該嘗試一下以找出問題所在。基本上,所有的字串都不會以相同的方式作業。這是我第一次使用拆分并開始于方法。我想就如何改善這種情況提出建議。另外,如何在沒有等號的情況下再次拆分它,只拆分內容?這是當前的代碼:
using System;
using System.IO;
namespace C_
{
class Program
{
static void Main(string[] args)
{
string title = "Naruto";
string description = "Two edgy boys and an annoying girl";
int totalEpisode = 720;
int currentEpisode = 69;
string folderPath = "D:/Folder"; // Write any path for folder
string path = "D:/Folder/text.txt"; // Write the path of above folder file name
if(File.Exists(path))
{
File.Delete(path); // Deletes File to prevent overwriting of same text
}
if (!Directory.Exists(folderPath)) // Creates Folder
{
Directory.CreateDirectory(folderPath);
}
StreamWriter writer = new StreamWriter(path, true); // Writes
writer.WriteLine(title "_title = " title ";");
writer.WriteLine(title "_description = " description ";");
writer.WriteLine(title "_totalEpisode = " totalEpisode ";");
writer.WriteLine(title "_currentEpisode = " currentEpisode ";");
writer.Close();
StreamReader reader = new StreamReader(path, true); // Reads
string allText = reader.ReadToEnd();
string[] texts = allText.Split(';');
string temp;
for(int i = 0; i < texts.Length; i )
{
temp = texts[i];
if(temp.StartsWith(title "_title = "))
{
title = temp;
Console.WriteLine("The title is: " title);
}
if (temp.StartsWith(title "_description = "))
{
description = temp;
Console.WriteLine("The description is: " description);
}
if (temp.StartsWith(title "_totalEpisode = "))
{
totalEpisode = System.Convert.ToInt32(temp);
Console.WriteLine("The Total Episodes are: " totalEpisode);
}
if (temp.StartsWith(title "_currentEpisode = "))
{
currentEpisode = System.Convert.ToInt32(temp);
Console.WriteLine("The Current Episode is: " currentEpisode);
}
}
reader.Close();
}
}
}
uj5u.com熱心網友回復:
temp = texts[i].Trim()從拆分中洗掉留在字串中的空格。- 當您檢查時
if temp.StartsWith(title etc..),它會失敗,因為您在第一個 if 分支中將標題重新分配給“Naruto_title = Naruto”,因此標題不會保持“火影忍者”。我們可以創建一個新變數,也可以像我在下面所做的那樣直接使用臨時變數。
using System;
using System.IO;
namespace C_
{
class Program
{
static void Main(string[] args)
{
string title = "Naruto";
string description = "Two edgy boys and an annoying girl";
int totalEpisode = 720;
int currentEpisode = 69;
string folderPath = "D:/Folder"; // Write any path for folder
string path = "D:/Folder/text.txt"; // Write the path of above folder file name
if(File.Exists(path))
{
File.Delete(path); // Deletes File to prevent overwriting of same text
}
if (!Directory.Exists(folderPath)) // Creates Folder
{
Directory.CreateDirectory(folderPath);
}
StreamWriter writer = new StreamWriter(path, true); // Writes
writer.WriteLine(title "_title = " title ";");
writer.WriteLine(title "_description = " description ";");
writer.WriteLine(title "_totalEpisode = " totalEpisode ";");
writer.WriteLine(title "_currentEpisode = " currentEpisode ";");
writer.Close();
StreamReader reader = new StreamReader(path, true); // Reads
string allText = reader.ReadToEnd();
string[] texts = allText.Split(';');
string temp;
for(int i = 0; i < texts.Length; i )
{
temp = texts[i].Trim();
if(temp.StartsWith(title "_title = "))
{
Console.WriteLine("The title is: " temp);
}
if (temp.StartsWith(title "_description = "))
{
Console.WriteLine("The description is: " temp);
}
if (temp.StartsWith(title "_totalEpisode = "))
{
Console.WriteLine("The Total Episodes are: " System.Convert.ToInt32(temp.Split("= ")[1]));
}
if (temp.StartsWith(title "_currentEpisode = "))
{
Console.WriteLine("The Current Episode is: " System.Convert.ToInt32(temp.Split("= ")[1]));
}
}
reader.Close();
}
}
}
uj5u.com熱心網友回復:
這是因為您使用了 StartsWith(),它應該以相同的開頭,但是在您的檔案中,第二行等以 \r\n 開頭,因此其他 ifs 永遠不匹配,您應該從字串中洗掉 '\r\n\或將它們包含在您的 StartsWith("\r\n" 隨便)
---按照@jh316 的建議進行編輯,您可以在 temp 上使用 trim()。
if (temp.TrimStart().StartsWith( title "_totalEpisode = ")){//youe code}
同樣在第一個 if() 你重新分配標題,你也應該重新修改
當你解決這些問題時,你也應該對你的演員表做一些事情。
totalEpisode = System.Convert.ToInt32(temp);
temp is = "\r\nNaruto_totalEpisode = 720" 我假設您只需要轉換 720,因此您應該根據 '=' 進行 som 拆分,然后轉換第二部分
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/392593.html
