This question already has answers here:
In Java, how do I check if a string contains a substring (ignoring case)? [duplicate]
(6 answers)
Closed 4 years ago.
I write program, which can recognize the certain word in the string, but i have a question. How to recognize a word in the string without gaps? (in one full string).
String string;
String word;
Scanner scan = new Scanner(System.in);
string = scan.nextLine();
word = scan.nextLine();
if(string.matches(".*\\b" + word +"\\b.*")){
System.out.println("found");
}
else {
System.out.println("Error");
}
If you don't need to actually get the exact string found, just replace your regex part with the following one:
if(string.matches(".*" + word + ".*")){
System.out.println("Found");
}
That should work.
As other validly pointed out, the matches function just tells whether the string matches the regex. If you need to check whether str1 contains str2, you should use contains() function.
Related
This question already has answers here:
How to check how many letters are in a string in java?
(5 answers)
Closed 2 years ago.
This is a simple question but I'm not very good at research.
I'm trying to find out the number of characters in a String and put it in an int variable. For example:
String word = "Hippo";
int numOfCharacters = (the code that tells me how many characters in the word);
And then use that number later.
The relevant JavaDoc will tell you that length()is the method you're looking for. So word.length(); would return the size of word, which is what you want.
Use the .length() to get the number of characters in a String
String word = "Hippo";
int numOfCharacters = word.length();
This question already has answers here:
How to get substrings from strings [duplicate]
(4 answers)
Closed 4 years ago.
I am new to Java. I want to ask how to search for a general sub-string within a given string.
For example:-
In the string 12345.67 I want to search for the sub-string .67
And in the string 1.00 I want to search for the string .00.
I basically want to search for the string after the radical (.), provided the number of characters after radical are only 2.
According to my knowledge search for general sub-string is not possible, I thereby asked for your help.
I wish to print the input (stored in the database) , a floating point number, into Indian Currency format, i.e, comma separated.
I even looked at various previous posts but none of them seemed to help me as almost everyone of them failed to produce the requite output for decimal point
According to my knowledge search for general sub-string is not possible
So you may learn a bit more, here String substring(int beginIndex) method :
String str = "12345.67";
String res = str.substring(str.indexOf('.')); // .67
If you want to check that there is only 2 digits after . :
String str = "12345.67";
String res = str.substring(str.indexOf('.') + 1); // 67
if(res.length() == 2)
System.out.println("Good, 2 digits");
else
System.out.println("Holy sh** there isn't 2 digits);
You can use split plus the substring to achieve your objective
String test = "12345.67";
System.out.println(test.split("\\.")[1].substring(0,2));
In the split function, you can pass the regex with which you could give the separator and in a substring function with the number of characters you want to extract
Next to the answer provided from #azro you may also use regex:
String string = "12345.67";
Pattern ppattern = Pattern.compile("\\d+(\\.\\d{2})");
Matcher matcher = pattern.matcher(string);
if(matcher.matches()){
String sub = matcher.group(1);
System.out.println(sub);
}
Which prints:
.67
String str = "12345.67";
String searchString = "." + str.split("\\.")[1];
if(str.contains(searchString)){
System.out.println("The String contains the subString");
}
else{
System.out.println("The String doesn't contains the subString");
}
This question already has answers here:
Make String first letter capital in java
(8 answers)
Closed 8 years ago.
I need to convert the first letter of a String into a Capital if it is not already one for part of a project of mine. Can anyone help me please?
Try using this,
String str= "haha";
str.replaceFirst("\\w", str.substring(0, 1).toUpperCase());
Try this
String s = "this is my string";
s.substring(0,1).toUpperCase();
In Java, this replaces every alpha-numeric word (plus underscores) so its first character is uppercase:
Matcher m = Pattern.compile("\\b([a-z])(\\w+)").matcher(str);
StringBuffer bfr = new StringBuffer();
while(m.find()) {
m.appendReplacement(bfr,
m.group(1).toUpperCase() + "$2");
}
m.appendTail(bfr);
It does not alter words that are already uppercased.
This question already has an answer here:
Split string on spaces in Java, except if between quotes (i.e. treat \"hello world\" as one token) [duplicate]
(1 answer)
Closed 8 years ago.
I have a String(freeText) "Manas \"Jaikant IBM\"". I want to split into two strings:
String normalMatch="Manas";
String exactMatch="Jaikant IBM";
It means that String normalMatch contains Manas and String exactMatch contains Jaikant IBM.
i am using the split() method of String class in Java
String[] splittedText= freeText.split("\\s");
I am getting 3 string elements but i need 2 string elements only.
Use substring instead of split:
int index = freeText.indexOf(" ");
String normalMatch = freeText.substring(0,index);
String exactMatch = freeText.substring(index); // endIndex == freeText.length())
Split on quotes(") then, you will get Manas and Jaikant IBM and can ignore the 3rd value.
This question already has answers here:
String.Split with a String?
(2 answers)
Closed 9 years ago.
I have a string "552 s hello"
I want to get the "552" in one substring and "s" in other substring and I want to split this String on the basis of first two spaces.Please help...
You can do it like this:
String t = "522 s hello"
String[] test = t.split(" ");
Now in the String array you will have three values, "522", "s" and "hello. And you can now access each.
Use String#split(String regex):
String[] sp="552 s hello".split(" ");
String fiveFiveTwo=sp[0];
String letterS=sp[1];
The names aren't very useful in the end, so you'll want to rename the strings I've created. You should also catch ArrayIndexOutOfBoundsException.