我想在vba中创建一个函数来从字符串中提取第n个单词并看起来像这样
ExtractWords(affected_text,delimiter,number_of_words_to_extract)
我尝试了一个解决方案,但它只提取前两个单词。
Function FirstWords(myStr As Variant, delimiter,words_to_extract) As Variant
FirstWords = Left(myStr, InStr(InStr(1, myStr, delimiter) + 1, myStr, delimiter, vbTextCompare) - 1)
End Function
有什么想法吗?感谢
答案 0 :(得分:3)
使用Split()功能。它返回public class SNCrawler extends Thread {
Specific s;
HashSet<String> hs = new HashSet<String>();
public SNCrawler(Specific s)
{
this.s = s;
}
public void crawl(String url) throws IOException {
Document doc = Jsoup.connect(url).get();
Elements links = doc.select("a");
for (Element link : links)
{
if(isSuitable(link.attr("href")) && !hs.contains(link.attr("abs:href")))
{
hs.add(link.attr("href"));
crawl(link.attr("href"));
}
}
}
public boolean isSuitable(String site)
{
boolean myBool = false;
if(site.startsWith("http://www.svensktnaringsliv.se/") && !SNFilter.matcher(site).matches())
if(site.contains(".pdf")) {
hs.add(site);
myBool=true;
}else{
hs.add(site);
myBool=true;
}
return myBool;
}
private static final Pattern SNFilter = Pattern.compile(".*((/staff/|medarbetare|play|/member_organizations/|/sme_committee/|rm=print|/contact/|/brussels-office/|/about-us|/newsletter/|/advantagesweden/|service=print|#)).*");
@Override
public void run()
{
try {
crawl("http://www.svensktnaringsliv.se/english/");
for(String myS : hs)
{
System.out.println(myS);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
数组,使用分隔符和您指定的单词限制进行拆分。
String
答案 1 :(得分:0)
@ Taosique使用Split
的回答非常好,但如果您希望将结果作为字符串返回,则可以执行以下操作:
Function FirstWords(myStr As String, delimiter As String, words_to_extract As Long) As Variant
Dim i As Long, k As Long
For i = 1 To Len(myStr)
If Mid(myStr, i, 1) = delimiter Then
k = k + 1
If k = words_to_extract Then
FirstWords = Mid(myStr, 1, i)
Exit Function
End If
End If
Next I
'if you get to here -- trouble
'unless the delimiter count is words_to_extract - 1
If k = words_to_extract - 1 Then
FirstWords = myStr
Else
FirstWords = CVErr(xlErrValue)
End If End Function
Sub test()
Debug.Print FirstWords("This is a test. I hope it works", " ", 4)
Debug.Print FirstWords("This is a test. I hope it works", " ", 10)
End Sub
当test
运行时,它首先显示字符串&#34;这是一个测试。&#34;然后输出错误条件。
通过首先使用Split
拆分字符串然后使用Join
重新加入字符串,可以实现与上述效果相同的效果。如果小于words_to_extract
个单词,则行为会有细微差别。 Split
然后Join
方法将返回整个字符串。上面的代码将此视为错误条件,如果用作UDF工作表函数,则会在包含它的任何单元格中显示#VALUE!
。