indexof

String.indexOf gives a value one less than expected

前提是你 提交于 2019-12-02 13:43:28
I do not understand the behaviour of the String.index Of method. String peter = "Peter Piper picked a peck of pickled pepper"; System.out.println("The number of p in the sentence is, " + peter.indexOf('p')); Why is the output of p 8 and not 9? There are 9 P's in the sentence. Indexes are zero-based: ↓ Peter Piper picked a peck of pickled pepper 111111111122222222223333333333444 0123456789012345678901234567890123456789012 ↑ The first p is at index 8 . From the javadoc of indexOf() : Returns the index of the first occurrence of the character in the character sequence represented by this object,

ArrayList of my objects, indexOf problem

久未见 提交于 2019-12-01 22:48:51
问题 I have problem with Java's ArrayList. I've created an Object, that contains two attributes, x and y. Now I've loaded some object in my ArrayList. Problem is that I don't know how to find index of some object with x atribute I'm searching. Is there any way to do this? 回答1: Assuming something like: public class Point { public final int x; public final int y; } And a declaration of: List<Point> points = ...; You can use for-each to iterate through all the points and find the one you want: for

JTextArea.select() does not select anything

邮差的信 提交于 2019-12-01 14:39:01
I am having troubles using the select function of a JTextArea . I tested the variables with System.out.print() if they are filled correctly. Everything seems good but the select function does not work at all. public class exercises extends JFrame { JTextField tf_search; String searchstr; JTextArea textarea; String aktStr; int Index; public exercises(String title) { super(title); setLayout(new FlowLayout()); JComboBox<String> combo = new JComboBox<String>(); combo.addItem("Here stands the whole Shit"); String[] systemfonts = new String[200]; systemfonts = GraphicsEnvironment

JTextArea.select() does not select anything

泄露秘密 提交于 2019-12-01 12:34:53
问题 I am having troubles using the select function of a JTextArea . I tested the variables with System.out.print() if they are filled correctly. Everything seems good but the select function does not work at all. public class exercises extends JFrame { JTextField tf_search; String searchstr; JTextArea textarea; String aktStr; int Index; public exercises(String title) { super(title); setLayout(new FlowLayout()); JComboBox<String> combo = new JComboBox<String>(); combo.addItem("Here stands the

Generic TList<> in Delphi 2009 crash on IndexOf

故事扮演 提交于 2019-12-01 08:41:09
I've seen many mentions of bugs in Delphi 2009 generics, but never expected something so basic to fail in Update 3, no less. Calling IndexOf on a generic TList or TObjectList causes an access violation if the list contains 1 or more items: type TTest = class( TObject ); procedure DoTest; var list : TObjectList< TTest >; t : TTest; begin list := TObjectList< TTest >.Create; try t := TTest.Create; list.IndexOf( t ); // No items in list, correct result -1 list.Add( t ); list.IndexOf( t ); // Access violation here finally list.Free; end; end; The exception is "EAccessViolation: Access violation at

Swift 5: Index of a Character in String

扶醉桌前 提交于 2019-12-01 06:12:42
Before Swift 5, I had this extension working: fileprivate extension String { func indexOf(char: Character) -> Int? { return firstIndex(of: char)?.encodedOffset } } Now, I get a deprecated message: 'encodedOffset' is deprecated: encodedOffset has been deprecated as most common usage is incorrect. Use `utf16Offset(in:)` to achieve the same behavior. Is there a simpler solution to this instead of using utf16Offset(in:) ? I just need the index of the character position passed back as an Int. And what is wrong with utf16Offset(in:) ? This is way to go with Swift 5 fileprivate extension String {

IndexOf method returns 0 when it should had return -1 in C# / Java

醉酒当歌 提交于 2019-12-01 04:14:14
A friend of mine came to me with this strange behavior which i can't explain, any insight view would be appreciated. Im running VS 2005 (C# 2.0), the following code show the behavior int rr = "test".IndexOf(""); Console.WriteLine(rr.ToString()); the above code, print "0" which clearly show it should have return -1 This also happen in Java where the following Class show the behavior: public class Test{ public static void main(String[] args){ System.out.println("Result->"+("test".indexOf(""))); } } Im running Java 1.6.0_17 This is not an exception to the rule, but rather a natural consequence of

Swift 5: Index of a Character in String

Deadly 提交于 2019-12-01 03:46:48
问题 Before Swift 5, I had this extension working: fileprivate extension String { func indexOf(char: Character) -> Int? { return firstIndex(of: char)?.encodedOffset } } Now, I get a deprecated message: 'encodedOffset' is deprecated: encodedOffset has been deprecated as most common usage is incorrect. Use `utf16Offset(in:)` to achieve the same behavior. Is there a simpler solution to this instead of using utf16Offset(in:) ? I just need the index of the character position passed back as an Int. 回答1:

Javascript find index of word in string (not part of word)

我的梦境 提交于 2019-12-01 03:23:09
I am currently using str.indexOf("word") to find a word in a string. But the problem is that it is also returning parts of other words. Example: "I went to the foobar and ordered foo." I want the first index of the single word "foo", not not the foo within foobar. I can not search for "foo " because sometimes it might be followed by a full-stop or comma (any non-alphanumeric character). You'll have to use regex for this: > 'I went to the foobar and ordered foo.'.indexOf('foo') 14 > 'I went to the foobar and ordered foo.'.search(/\bfoo\b/) 33 /\bfoo\b/ matches foo that is surrounded by word

How to compare cells in JavaScript table to each other and test for equality? How does indexOf work?

懵懂的女人 提交于 2019-12-01 01:53:02
I created a table in my HTML code. It has 9 columns and 13 rows. It gets filled up completely by a JavaScript loop that fills it with names of people from a few arrays. However, I want to add a validation step that makes sure that no two cells within a row hold the same value and that the value of a each cell does not repeat in the cell directly beneath it. Since I am only able to access the values of the cells of the table as a NodeList, I decided to make it into an array to use the IndexOf property to search through the array: var table1 = document.getElementsByTagName("td"); var table1Array