Skip to content
Advertisement

Tag: string

Set running time limit on a method in java

I have a method that returns a String. Is it possible that after a certain time, if threshold is excedeed for that method, to return some specific string? Answer The Guava library has a very nice TimeLimiter that lets you do this on any method that’s defined by an interface. It can generate a proxy for your object that has

Ultra-fast “Begins With” Query from Disk

I have a 40MB (too big for memory in this case) list of strings that I want to do “begins with” queries on to extract matches. Anyone know of a good data structure for this? Bonus points for an existing os java implementation. I would be willing to sacrifice “begins with” to just exact matching if something already exists. A

Remove all empty lines

I thought that wasn’t that hard to do, but I want to remove all empty lines (or lines just containing blanks and tabs in Java) with String.replaceAll. My regex looks like this: But it doesn’t work. I looked around, but only found regexes for removing empty lines without blanks or tabs. Answer Try this: Note that the regex [ |t]

How can I find whitespace in a String?

How can I check to see if a String contains a whitespace character, an empty space or ” “. If possible, please provide a Java example. For example: String = “test word”; Answer For checking if a string contains whitespace use a Matcher and call its find method. If you want to check if it only consists of whitespace then

Search for a word in a String

If I am looking for a particular word inside a string, for example, in the string “how are you” I am looking for “are”. Would a regular indexOf() work faster and better or a Regex match() Which of the two methods above is a better way of looking for a string inside another string? Or is there a much better

Advertisement