我试图找到一种方法来正确地划分这一点,但到目前为止,我遇到了许多问题。
使用string.split / string.substring, string.indexof, string.replace等。
下面是一个需要拆分成列表的示例字符串。
We are <b><i>very</i></b><b>a</b>mused!\nThank you.列表中的结果应该是这样的:
0: We
1: are
2: <b>
3: <i>
4: very
5: </i>
6: </b>
7: <b>
8: a
9: </b>
10: mused!
11: \n
12: Thank
13: you.所以我想要做的是:
splitStart = baseString.Value.Split(' ');
foreach (string part in splitStart)
{
if (part.Contains("<"))
{
// get the parts <b> <i> <size> <color> </b> </i> </size> </color> \n
textlist.Add(part); // add each part to list
}
else
{
textlist.Add(part);
Debug.Log(part);
}
}我试过这样的东西
contains("<n>")
replace "<n>" "" and add "<n>" to array但这可能会打破这一顺序。
编辑:我忘了说这是为c#编写的
发布于 2017-09-05 14:59:07
我认为你需要使用一些像jsoup或树结构算法这样的html解析器对字符进行一些预处理。
这是使用Jsoup库实现这种情况的一种选择。
1. Java版本
首先,从html标签准备单词列表。
final List<String> wordList = new ArrayList<String>();然后,使用Jsoup的NodeVisitor类遍历html内容。
doc.body().traverse(
new NodeVisitor(){
@Override
public void head(Node arg0, int arg1) {
if(arg1 == 1)
{
String value = arg0.outerHtml();
if(!wordList.contains(value))
wordList.add(arg0.outerHtml());
}
}
@Override
public void tail(Node arg0, int arg1) {
}
}
);最后,代码如下。
import java.util.ArrayList;
import java.util.List;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.select.Elements;
import org.jsoup.select.NodeVisitor;
public class HtmlTest {
public static String parseHtml( String str ) {
org.jsoup.nodes.Document doc = Jsoup.parse(str);
final List<String> wordList = new ArrayList<String>();
doc.body().traverse(
new NodeVisitor(){
@Override
public void head(Node arg0, int arg1) {
if(arg1 == 1)
{
//String value = Jsoup.parse(arg0.outerHtml()).text();
String value = arg0.outerHtml();
if(!wordList.contains(value))
wordList.add(arg0.outerHtml());
}
}
@Override
public void tail(Node arg0, int arg1) {
}
}
);
for(String word: wordList)
{
System.out.println(word);
}
return "";
}
public static void main(String[] args)
{
System.out.println(parseHtml( "We are <b><i>very</i></b><b>a</b>mused!\nThank you." ));
}
}输出必须如下所示:
We are
<b><i>very</i></b>
<b>a</b>
mused! Thank you.2. C#版本
嗯,C#版本的源代码有一点不同,但过程是一样的(只需要一点改动)。
这是我的NodeVisitor版本的代码。
首先解析html内容。
Document doc = NSoupClient.Parse(str);第二,从“body”标签中选择原句。
doc.Select("body").Traverse(new TestNodeVisitor(wordList));完整的代码如下。
using NSoup;
using NSoup.Nodes;
using NSoup.Select;
using System;
using System.Collections.Generic;
using System.IO;
namespace NSoupTest
{
class Program
{
private class TestNodeVisitor : NodeVisitor
{
List<String> wordList;
public TestNodeVisitor(List<String> wordList)
{
this.wordList = wordList;
}
public void Head(Node node, int depth)
{
if(depth == 1)
{
String value = node.OuterHtml();
if(!wordList.Contains(value))
wordList.Add(value);
}
}
public void Tail(Node node, int depth)
{
}
}
public static String parseHtml( String str ) {
Document doc = NSoupClient.Parse(str);
List<String> wordList
= new List<String>();
doc.Select("body").Traverse(new TestNodeVisitor(wordList));
foreach (String word in wordList)
{
Console.WriteLine(word);
}
return "";
}
static void Main(string[] args)
{
try
{
parseHtml("We are <b><i>very</i></b><b>a</b>mused!\nThank you.");
}
catch (FileNotFoundException fe) {
Console.WriteLine(fe.Message);
}
}
}
}输出也应该是
We are
<b><i>very</i></b>
<b>a</b>
mused! Thank you.您可以从site中找到我当时使用的NSoup库(实际上,不是官方版本0.8.0)。
官方的NSoup站点是here,但没有访问者界面。
然后,您可以使用自己的方法来完成代码。
我必须告诉你,这只是你的目标的一种选择。
尊敬的,
https://stackoverflow.com/questions/46047126
复制相似问题