简体   繁体   English

使用Java中的边界查找多个子字符串

[英]Finding multiple substrings using boundaries in Java

Alright so here is my problem. 好吧,这是我的问题。 Basically I have a string with 4 words in it, with each word seperated by a #. 基本上,我有一个包含4个单词的字符串,每个单词用#分隔。 What I need to do is use the substring method to extract each word and print it out. 我需要做的是使用substring方法提取每个单词并打印出来。 I am having trouble figuring out the parameters for it though. 我在为它找出参数时遇到了麻烦。 I can always get the first one right, but the following ones generally have problems. 我总是可以正确选择第一个,但是以下几个通常会有问题。 Here is the first piece of the code: 这是代码的第一部分:

word = format.substring( 0 , format.indexOf('#') );

Now from what I understand this basically means start at the beginning of the string, and end right before the #. 现在,据我了解,这基本上意味着从字符串的开头开始,并在#之前结束。 So using the same logic, I tried to extract the second word like so: 因此,使用相同的逻辑,我尝试提取第二个单词,如下所示:

wordTwo = format.substring ( wordlength + 1 , format.indexOf('#') ); 
//The plus one so I don't start at the #.

But with this I continually get errors saying it doesn't exist. 但是与此同时,我不断收到错误消息,指出它不存在。 I figured that the compiler was trying to read the first # before the second word, so I rewrote it like so: 我认为编译器正在尝试在第二个单词之前读取第一个#,因此我将其重写为:

wordTwo = format.substring (wordlength + 1, 1 + wordLength + format.indexOf('#') );

And with this it just completely screws it up, either not printing the second word or not stopping in the right place. 这样一来,它就完全拧紧了,要么不打印第二个单词,要么没有停在正确的位置。 If I could get any help on the formatting of this, it would be greatly appreciated. 如果我能在格式化方面获得任何帮助,将不胜感激。 Since this is for a class, I am limited to using very basic methods such as indexOf, length, substring etc. so if you could refrain from using anything to complex that would be amazing! 由于这是针对一类的,因此我仅限于使用非常基本的方法,例如indexOf,length,substring等。因此,如果您可以避免使用任何复杂的东西,那就太好了!

If you have to use substring then you need to use the variant of indexOf that takes a start . 如果必须使用子字符串,则需要使用开始的indexOf变体 This means you can start look for the second # by starting the search after the first one. 这意味着您可以通过在第一个#之后开始搜索来开始寻找第二个# Ie

wordTwo = format.substring ( wordlength + 1 , format.indexOf('#', wordlength + 1 ) );

There are however much better ways of splitting a string on a delimiter like this. 但是,有很多更好的方法可以在这样的分隔符上分割字符串。 You can use a StringTokenizer . 您可以使用StringTokenizer This is designed for splitting strings like this. 这是为拆分字符串而设计的。 Basically: 基本上:

StringTokenizer tok = new StringTokenizer(format, "#");
String word = tok.nextToken();
String word2 = tok.nextToken();
String word3 = tok.nextToken();

Or you can use the String.split method which is designed for splitting strings. 或者,您可以使用专门用于拆分字符串的String.split方法。 eg 例如

String[] parts = String.split("#");
String word = parts[0];
String word2 = parts[1];
String word3 = parts[2];

You can go with split() for this kind of formatting strings. 您可以使用split()获得这种格式的字符串。

For instance if you have string like, 例如,如果您有类似字符串,

String text = "Word1#Word2#Word3#Word4";

You can use delimiter as, 您可以将定界符用作,

String delimiter = "#";

Then create an string array like, 然后创建一个字符串数组,例如

  String[] temp;

For splitting string, 对于分割字符串,

temp = text.split(delimiter);

You can get words like this, 你可以得到这样的话,

temp[0] = "Word1";
temp[1] = "Word2";
temp[2] = "Word3";
temp[3] = "Word4";

Use split() method to do this with "#" as the delimiter 使用split()方法以"#"作为分隔符来执行此操作

String  s = "hi#vivek#is#good";
String temp = new String();

String[] arr = s.split("#");

for(String x : arr){

  temp = temp + x;
}

Or if you want to exact each word ... you have it already in arr 或者,如果您想精确每个单词 ...您已经在arr

arr[0] ---> First Word
arr[1] ---> Second Word
arr[2] ---> Third Word

I suggest that you've a look at the Javadoc for String before you proceed further. 我建议您先阅读StringJavadoc,然后再继续。

Since this is your homework, I'll give you a couple of hints and maybe you can solve it yourself: 由于这是您的作业,因此我会给您一些提示,也许您可​​以自己解决:

  • The format for subString is public void subString(int beginIndex, int endIndex) . subString的格式为public void subString(int beginIndex, int endIndex) As per the javadoc for this method: 按照此方法的javadoc

Returns a new string that is a substring of this string. 返回一个新字符串,该字符串是该字符串的子字符串。 The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex. 子字符串从指定的beginIndex开始,并扩展到索引endIndex-1处的字符。因此,子字符串的长度为endIndex-beginIndex。

Note that if you've to use this method, understand that you'll have to shift your beginIndex and endIndex each time because in your situation, you'll have multiple words that are separated by #. 请注意,如果您必须使用此方法,请理解您每次都必须移动beginIndex和endIndex,因为在您的情况下,您将有多个由#分隔的单词。

  • However if you look closely, there's another method in String class that might be helpful to you. 但是,如果仔细观察,String类中还有另一个方法可能对您有所帮助。 That's the public String[] split(String regex) method. 那就是public String[] split(String regex)方法。 The javadoc for this one states: Javadoc指出:

Splits this string around matches of the given regular expression. 围绕给定正则表达式的匹配项拆分此字符串。 This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. 该方法的工作方式就像通过调用具有给定表达式且限制参数为零的二参数拆分方法。 Trailing empty strings are therefore not included in the resulting array. 因此,结尾的空字符串不包括在结果数组中。

The split() method looks pretty interesting for your case. 对于您的情况, split()方法看起来非常有趣。 You can split your String with the delimiter that you have as the parameter to this method, get the String array and work with that. 您可以使用该方法的参数分隔符来分割 String,获取String数组并对其进行处理。

Hope this helps you to understand your problem and get started towards a solution :) 希望这可以帮助您理解问题并开始寻求解决方案:)

Since this is a home work, it may be better to have try to write it your self. 由于这是家庭作业,因此尝试自己编写可能会更好。 But I will give a clue. 但我会提供一个线索。

Clue: 线索:

The indexOf method has another overload: int indexOf(int chr, int fromIndex) which find the first character chr in the string from the fromIndex . indexOf方法还有另一个重载: int indexOf(int chr, int fromIndex)fromIndex查找字符串中的第一个字符chr

http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html

From this clue, the program will look something like this: 根据此线索,该程序将如下所示:

  1. Find the index of the first '#' from the start of the string. 从字符串的开头查找第一个'#'的索引。
  2. Extract the word from 0th character to that index. 从第0个字符中提取单词到该索引。
  3. Find the index of the first '#' from the character AFTER the first '#' . 找到第一个的索引'#'从字符后的第一个'#'
  4. Extract the word from the first '#' that index. 从该索引的第一个'#'中提取单词。 ... Just do it until you get 4 words or the string ends. ...直到您得到4个单词或字符串结束为止。

Hope this helps. 希望这可以帮助。

I don't know why you're forced to use String#substring , but as others have mentioned, it seems like the wrong method for the kind of functionality you need. 我不知道为什么您被迫使用String#substring ,但是正如其他人提到的那样,对于所需的功能来说,这似乎是错误的方法。

String#split(String regex) is what you would use for such a problem, or, if your input sequence is something you don't control, I would suggest you look at the overloaded method String#split(String regex, int limit) ; String#split(String regex)是用于解决此问题的方法,或者,如果输入序列不受控制,建议您查看重载的方法String#split(String regex, int limit) ; this way you can impose a limit on the amount of matches you make, controlling your resulting array. 这样,您可以限制匹配的数量,从而控制结果数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM