I want to search a word inside a string :
For example let the String be "ThisIsFile.java"
and let i want to search "File.java" or "IsFile"
This is something like sql 'like' query but unfortunately i am not
getting that string from database.
Please suggest me any good solution for this.
Thanks
There's a variety of ways of achieving this and the method you choose will depend on the complexity of your queries. For a simple plain symbol/word match the String class provides a contains method that takes a single parameter, the String to search for - and returns true if it occurs within the search String.
bool containsFile = myString.contains("file");
Do you mean:
if (haystack.contains(needle))
? Note that this won't respect word boundaries or anything like that - it just finds if one string (needle) is a substring in another (haystack).
Try using String.contains(). Also, check out the documentation for more information about the method
You should be able to use
String.contains("some string");
Related
Currently I am working on a project and I am trying to see which String method would be most appropriate to use or how to approach this. I am trying to prepend a string to each occurrence of this specific string. For example, I am extracting HTML and for each /img/image1.png I find I want to append a url to it.
However, there are images that are already like that for example www.anylink.com/img/image2.png which do not need appending but are in the string in which I pulled. I looked at replaceAll() method but not sure if this allows for appending in replacement and also not sure if I need regex to search for instances where only /img/ exists(no url) and not the full url since only local hosted images I want to append to. I am looking for some suggestions as I am not sure how to begin this code after research.
Thank you.
I think that the method replaceAll() in String is enough for what you need.
You just need to write the correct regular expression.
If you write some examples, I can suggest the regex.
For example something like:
System.out.println("<div><img src=\"/test/this.png\" /></div>".replaceAll("src=\"/(.*)\"", "src=\"www.google.com$1\""));
We use a library which uses the regular expression
Pattern.compile("^\\w+(\\.\\w+)*$")
which is used to validate a string .
For example abc.xyz is valid string and it passes through the validation.
As a workaround for another issue i need provide the string as abc.xyz,efg.ghi, which obviously does not get past the regex validation.Is there a way to make this string pass through the validation and if yes, how ?
PS: I tried using the escape sequences abc.xyz\\,efg\\.ghi. It did not work .
Just put comma and dot inside a character class.
Pattern.compile("^\\w+([,.]\\w+)*$");
DEMO
As it stands now, you can't pass in , characters. However, if there really is no way to change the library (e.g. proprietary), you can abuse Java's String cache + reflection to change the String literal before the proprietary class is loaded.
I have these tags in my textarea
<gras>.....</gras>
And I'm trying to replace them using the replaceAll() String method
text.replaceAll("<gras>", "<b>");
text.replaceAll("</gras>", "</b>");
But, this regex code doesn't work. Any help please ?
You forgot a very important concept;
.
Change text.replaceAll("<gras>", "Bold!");
To
text = text.replaceAll("<gras>", "Bold!");
Assign text = some Function, as text.replace() is creating a new String object and not referencing it.
Hope this helps.
Strings don't replace. Strings construct new Strings with the replacement values.
Also, if you are dealing with XML, regex is the wrong tool. That doesn't mean it can't work, and it might be useful in some limited examples, but it shouldn't be the first tool to use. Much like a hammer shouldn't be the first tool to use when installing a screw.
I am getting Messages from Server So that I need to Parse the Contents of the Messages.
Currently I received the Message and converted it into the String specified below,
Lat:+1290.9890N
Long:+9098.987890E
Spd:90km/h
So from the above String, I need to get the Value of Lat,Long and speed ..How to do that in Java..Thanks in advance..
There are many ways, use substring function http://docs.oracle.com/javase/6/docs/api/java/lang/String.html. Try by your self.
You can use split, substring or other ways to do it. I would recommend to use pojo for this to avoid any issues in processing string like Sting index out of bounds other possible exceptions. It will be cleaner and easier to manage as your message seems pretty static in its format.
Create a string builder for each line.
Iterate through each existing string.
Call isDigit() on each item in the string.
If isDigit == true, append the digit to the string builder.
Cast the string builder to a long
You could create a Scanner and call findInLine() with a Pattern regular expression that will only find floating point numbers e.g. (0..9)*.(0..9)*
I am doing string manipulations and I need more advanced functions than the original ones provided in Java.
For example, I'd like to return a substring between the (n-1)th and nth occurrence of a character in a string.
My question is, are there classes already written by users which perform this function, and many others for string manipulations? Or should I dig on stackoverflow for each particular function I need?
Check out the Apache Commons class StringUtils, it has plenty of interesting ways to work with Strings.
http://commons.apache.org/lang/api-2.3/index.html?org/apache/commons/lang/StringUtils.html
Have you looked at the regular expression API? That's usually your best bet for doing complex things with strings:
http://download.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html
Along the lines of what you're looking to do, you can traverse the string against a pattern (in your case a single character) and match everything in the string up to but not including the next instance of the character as what is called a capture group.
It's been a while since I've written a regex, but if you were looking for the character A for instance, then I think you could use the regex A([^A]*) and keep matching that string. The stuff in the parenthesis is a capturing group, which I reference below. To match it, you'd use the matcher method on pattern:
http://download.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html#matcher%28java.lang.CharSequence%29
On the Matcher instance, you'd make sure that matches is true, and then keep calling find() and group(1) as needed, where group(1) would get you what is in between the parentheses. You could use a counter in your looping to make sure you get the n-1 instance of the letter.
Lastly, Pattern provides flags you can pass in to indicate things like case insensitivity, which you may need.
If I've made some mistakes here, then someone please correct me. Like I said, I don't write regexes every day, so I'm sure I'm a little bit off.