Java中最接近NSScanner的东西
我正在将一些代码从objective-c迁移到java。该项目是一个XML/HTML解析器。在objective c中,我几乎只使用ScanupString(“mystring”);方法 我查看了Java Scanner类,但它将所有内容都分解为令牌。我不想那样。我只想能够扫描到子字符串的出现,并跟踪扫描器在整个字符串中的当前位置 任何帮助都很好,谢谢 编辑 更具体地说。我不希望Scanner标记化Java中最接近NSScanner的东西,java,string,string-parsing,nsscanner,Java,String,String Parsing,Nsscanner,我正在将一些代码从objective-c迁移到java。该项目是一个XML/HTML解析器。在objective c中,我几乎只使用ScanupString(“mystring”);方法 我查看了Java Scanner类,但它将所有内容都分解为令牌。我不想那样。我只想能够扫描到子字符串的出现,并跟踪扫描器在整个字符串中的当前位置 任何帮助都很好,谢谢 编辑 更具体地说。我不希望Scanner标记化 String test = "<title balh> blah <title
String test = "<title balh> blah <title> blah>";
Scanner feedScanner = new Scanner(test);
String title = "<title";
String a = feedScanner.next(title);
String b = feedScanner.next(title);
String test=“blah blah>”;
扫描器进给扫描器=新扫描器(测试);
String title=“也许String.split适合你
s = "The almighty String is mystring is your String is our mystring-object - isn't it?";
parts = s.split ("mystring");
结果:
Array("The almighty String is ", " is your String is our ", -object - isn't it?)
你知道在你的“mystring”之间必须是。我不确定开始和结束,所以也许你需要一些s.startsWith(“mystring”)/s.endsWith
也许String.split适合你
s = "The almighty String is mystring is your String is our mystring-object - isn't it?";
parts = s.split ("mystring");
结果:
Array("The almighty String is ", " is your String is our ", -object - isn't it?)
您知道在“mystring”之间必须是。我不确定开始和结束,所以您可能需要一些s.startsWith(“mystring”)/s.endsWith
您可以使用String类(Java.lang.String)实现这一点
首先获取子字符串的第一个索引
int first\u occurrence=string.indexOf(子字符串)
然后迭代整个字符串并获得子字符串的下一个值
int next_index=indexOf(str,fromIndex)
如果要保存这些值,请将它们添加到包装器类中,并添加到arraylist对象中
您可以通过String类(Java.lang.String)实现这一点
首先获取子字符串的第一个索引
int first\u occurrence=string.indexOf(子字符串)
然后迭代整个字符串并获得子字符串的下一个值
int next_index=indexOf(str,fromIndex)
如果要保存这些值,请将它们添加到包装器类中,并添加到arraylist对象中
直接使用String
的方法确实更容易:
String test = "<title balh> blah <title> blah>";
String target = "<title";
int index = 0;
index = test.indexOf( target, index ) + target.length();
// Index is now 6 (the space b/w "<title" and "blah"
index = test.indexOf( target, index ) + target.length();
// Index is now at the ">" in "<title> blah"
直接使用String
的方法确实更容易:
String test = "<title balh> blah <title> blah>";
String target = "<title";
int index = 0;
index = test.indexOf( target, index ) + target.length();
// Index is now 6 (the space b/w "<title" and "blah"
index = test.indexOf( target, index ) + target.length();
// Index is now at the ">" in "<title> blah"
你能更具体地说明什么是Scanner
不允许你做的吗?从你所写的来看,甚至Scanner
都是多余的,你可以使用。你能更具体地说明什么是Scanner
不允许你做的吗?从你所写的来看,甚至Scanner
都是多余的,你可以使用。