indexof

How to use substring and indexOf for a String with repeating characters?

拜拜、爱过 提交于 2019-12-07 13:50:59
问题 I have the following String myString="city(Denver) AND state(Colorado)"; It has repeating "(" and ")"... How can I retrieve state name, i.e. Colorado. I tried the following: String state = myString.substring(myString.indexOf("state(")+1,myString.indexOf(")")); But it give indexOutOfBoundException Is there any way to specify that I need the second "(" in myString? I need the result: String state = "Colorado"; 回答1: Use lastIndexOf . Also increase the initial offset to allow for the number of

(C#) Improving speed of custom getBetweenAll

半世苍凉 提交于 2019-12-07 08:15:09
问题 I've written a custom extension method in c# that is an improvement of the extensionmethod string[] getBetweenAll(string source, string startstring, string endstring); Originally this extensionmethod found all substrings between two strings, for example: string source = "<1><2><3><4>"; source.getBetweenAll("<", ">"); //output: string[] {"1", "2", "3", "4"} But if you had another occurrence of < in the beginning it would just get between that and the whole string string source = "<<1><2><3><4>

Looping Through String To Find Multiple Indexes

▼魔方 西西 提交于 2019-12-06 07:05:58
问题 I am trying to figure out the most efficient way of looping through a string and finding all indexes of a certain letter. I have used $word_or_phrase.indexOf( $letter ); to find a single index of a letter, but the letter is in $word_or_phrase multiple times. Would the most efficient way to do this be to build an array of all the indexes until .indexOf returns -1? Or how would you suggest me finding all the the indexes? I have already taken time and found this: Javascript str.search() multiple

Array indexOf implementation for Internet Explorer

前提是你 提交于 2019-12-06 03:53:56
There are plenty of solutions on how to get the indexOf implementation into the Array prototype so that it works under Internet Explorer, however I've stumbled upon an issue that doesn't seem to be addressed anywhere I've looked so far. Using the pretty well agreed upon implementation at MDC , I have the following code that's being problematic now: // indexOf support for IE (from MDC) if (!Array.prototype.indexOf) { Array.prototype.indexOf = function(elt /*, from*/) { var len = this.length >>> 0; var from = Number(arguments[1]) || 0; from = (from < 0) ? Math.ceil(from) : Math.floor(from); if

indexOf to find all occurrences of a word in a String

别说谁变了你拦得住时间么 提交于 2019-12-05 21:54:55
I'm trying to use indexOf to find all occurrences of the characters 'the' in a sentence. For example, if the sentence were "The other day I went over there", it should return 3. I am able to do this up to the point where it finds the first index, but I'm unsure of how to write the loop. I originally had a for loop that searched the entire string, but it was returning the full string character length, instead of the occurrences of my specified character. How can I write a loop that will find all of the occurrences of the word? Thank you. import java.util.Scanner; public class TheFinder { public

How to return the next indexOf after a previous?

房东的猫 提交于 2019-12-05 18:49:02
For example: str = "(a+b)*(c+d)*(e+f)" str.indexOf("(") = 0 str.lastIndexOf("(") = 12 How to get the index in second bracket? (c+d) <- this Try this : String word = "(a+b)*(c+d)*(e+f)"; String c = "("; for (int index = word.indexOf(c);index >= 0; index = word.indexOf(c, index + 1)) { System.out.println(index);//////here you will get all the index of "(" } wrm int first = str.indexOf("("); int next = str.indexOf("(", first+1); have a look at API Documentation You can use StringUtils from Apache Commons, in this case it would be StringUtils.indexof(str, ")", str.indexOf(")") + 1); The idea is

How to use substring and indexOf for a String with repeating characters?

狂风中的少年 提交于 2019-12-05 18:32:02
I have the following String myString="city(Denver) AND state(Colorado)"; It has repeating "(" and ")"... How can I retrieve state name, i.e. Colorado. I tried the following: String state = myString.substring(myString.indexOf("state(")+1,myString.indexOf(")")); But it give indexOutOfBoundException Is there any way to specify that I need the second "(" in myString? I need the result: String state = "Colorado"; Use lastIndexOf . Also increase the initial offset to allow for the number of characters in the sub-string state( : String state = myString.substring(myString.indexOf("state(") + 6,

(C#) Improving speed of custom getBetweenAll

一世执手 提交于 2019-12-05 13:53:53
I've written a custom extension method in c# that is an improvement of the extensionmethod string[] getBetweenAll(string source, string startstring, string endstring); Originally this extensionmethod found all substrings between two strings, for example: string source = "<1><2><3><4>"; source.getBetweenAll("<", ">"); //output: string[] {"1", "2", "3", "4"} But if you had another occurrence of < in the beginning it would just get between that and the whole string string source = "<<1><2><3><4>"; source.getBetweenAll("<", ">"); //output: string[] {"<1><2><3><4"} So I re-wrote it to be more exact

Getting the number of occurrences of one string in another string

半城伤御伤魂 提交于 2019-12-05 11:25:45
I need to input a two strings, with the first one being any word and the second string being a part of the previous string and i need to output the number of times string number two occurs. So for instance:String 1 = CATSATONTHEMAT String 2 = AT. Output would be 3 because AT occurs three times in CATSATONTHEMAT. Here is my code: public static void main(String[] args) { Scanner sc = new Scanner(System.in); String word8 = sc.next(); String word9 = sc.next(); int occurences = word8.indexOf(word9); System.out.println(occurences); } It outputs 1 when I use this code. You could also try: public

IE: indexOf results in “object doesn't support this property or method”

倖福魔咒の 提交于 2019-12-05 09:01:38
问题 I have the following if statement: if (buyArray.indexOf(dealWith,0) != -1){ Which is breaking in ie (ie 8 on XP) with "object doesn't support this property or method". Anyone have a work around for this? 回答1: yeah, IE<9 doesn't support indexOf . You can implement a shim like the one showed here: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf Or if you already using jQuery you can use inArray. Also underscore has an implementation for it. 回答2: Simply changed