用几个包含字符串替换 base64 字符串的图像路径
Replace image path to base64 string with several containing string
我有一个包含“https://tryout.pendukasi.id/upload/" and I want to replace every string that contains "https://tryout.pendukasi.id/upload/”到"data:image/jpeg; base64"的字符串。
代码:
foreach (StorageFile file in files)
{
string soalP = file.Path;
if (question.Pertanyaan.ToString().Contains("https://tryout.pendidikan.id/upload/"))
{
byte[] imageArray = System.IO.File.ReadAllBytes(soalP);
string base64ImageRepresentation = Convert.ToBase64String(imageArray);
soal = Regex.Replace(question.Pertanyaan.ToString(), "\"https://tryout.pendidikan.id/upload/" + file.Name + "\"", "data:image/jpeg;base64," + base64ImageRepresentation);
}
}
我有一个问题,如果字符串包含一个“https://tryout.pendukasi.id/upload/", then the string is successfully replaced, but if it contains several "https://tryout.pendukasi.id/upload/”,例如:"<p>Perhatikan ayat-ayat Surat al-Falaq berikut ini!<br /><img src=\"https://tryout.pendidikan.id/upload/3-1.JPG\" /><br /><img src=\"https://tryout.pendidikan.id/upload/3-2.JPG\" /><br /><img src=\"https://tryout.pendidikan.id/upload/3-3.JPG\" /><br /><img src=\"https://tryout.pendidikan.id/upload/3-4.JPG\" /><br /><img src=\"https://tryout.pendidikan.id/upload/3-5.JPG\" /><br />Urutan ayat dalam Surat al-Falaq yang benar adalah ….</p>"
,则字符串没有成功更改。如何解决这个问题?
发生这种情况是因为您正在逐个文件替换它,并且它之前的更改不断被更改:
soal = Regex.Replace(question.Pertanyaan.ToString(), "\"https://tryout.pendidikan.id/upload/" + file.Name + "\"", "data:image/jpeg;base64," + base64ImageRepresentation);
一种方法是将您的正则表达式替换字符串保存到一个变量中。
这样,您的替换内容就不会被覆盖:
string soal = question.Pertanyaan.ToString();
foreach (StorageFile file in files)
{
string soalP = file.Path;
if (soal.Contains("https://tryout.pendidikan.id/upload/"))
{
byte[] imageArray = System.IO.File.ReadAllBytes(soalP);
string base64ImageRepresentation = Convert.ToBase64String(imageArray);
soal = Regex.Replace(soal, "\"https://tryout.pendidikan.id/upload/" + file.Name + "\"", "data:image/jpeg;base64," + base64ImageRepresentation);
}
}
我有一个包含“https://tryout.pendukasi.id/upload/" and I want to replace every string that contains "https://tryout.pendukasi.id/upload/”到"data:image/jpeg; base64"的字符串。
代码:
foreach (StorageFile file in files)
{
string soalP = file.Path;
if (question.Pertanyaan.ToString().Contains("https://tryout.pendidikan.id/upload/"))
{
byte[] imageArray = System.IO.File.ReadAllBytes(soalP);
string base64ImageRepresentation = Convert.ToBase64String(imageArray);
soal = Regex.Replace(question.Pertanyaan.ToString(), "\"https://tryout.pendidikan.id/upload/" + file.Name + "\"", "data:image/jpeg;base64," + base64ImageRepresentation);
}
}
我有一个问题,如果字符串包含一个“https://tryout.pendukasi.id/upload/", then the string is successfully replaced, but if it contains several "https://tryout.pendukasi.id/upload/”,例如:"<p>Perhatikan ayat-ayat Surat al-Falaq berikut ini!<br /><img src=\"https://tryout.pendidikan.id/upload/3-1.JPG\" /><br /><img src=\"https://tryout.pendidikan.id/upload/3-2.JPG\" /><br /><img src=\"https://tryout.pendidikan.id/upload/3-3.JPG\" /><br /><img src=\"https://tryout.pendidikan.id/upload/3-4.JPG\" /><br /><img src=\"https://tryout.pendidikan.id/upload/3-5.JPG\" /><br />Urutan ayat dalam Surat al-Falaq yang benar adalah ….</p>"
,则字符串没有成功更改。如何解决这个问题?
发生这种情况是因为您正在逐个文件替换它,并且它之前的更改不断被更改:
soal = Regex.Replace(question.Pertanyaan.ToString(), "\"https://tryout.pendidikan.id/upload/" + file.Name + "\"", "data:image/jpeg;base64," + base64ImageRepresentation);
一种方法是将您的正则表达式替换字符串保存到一个变量中。 这样,您的替换内容就不会被覆盖:
string soal = question.Pertanyaan.ToString();
foreach (StorageFile file in files)
{
string soalP = file.Path;
if (soal.Contains("https://tryout.pendidikan.id/upload/"))
{
byte[] imageArray = System.IO.File.ReadAllBytes(soalP);
string base64ImageRepresentation = Convert.ToBase64String(imageArray);
soal = Regex.Replace(soal, "\"https://tryout.pendidikan.id/upload/" + file.Name + "\"", "data:image/jpeg;base64," + base64ImageRepresentation);
}
}