Reading each word from Email addresses in Java [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an email address as a String and from that I have to find certain words if Email is containing. I have to find the words before, after and, in between based on these:-
. , # , - , _ .
My Code:
String s = "asdfgabc#gmail.com";
String var = null;
for(int i = 0; i < s.length(); i++)
{
char c = s.charAt(i);
var+=c;
if(var.contains("#") || var.contains(".") || var.contains("-") || var.contains("_") ){
//System.out.println(var);
}
else{
var+=c;
System.out.println(var);
}
}

Simply split the string and then process your words :
String[] words = "bad.guy#nowhere.net".split("[._#-]");
Arrays.println(words);
// >> [bad, guy, nowhere, net]
// Process the output word by word

Related

How to get the count of specific letters in a String? (Android) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 months ago.
Improve this question
I need to count the number of times a letter's present in a String.
For example:
str = "/data/name/data/name"
How do we get the number of / in this string?
val count = str.count { it == '/' }
To be honest, I am not sure whether you need an answer in java or kotlin (your tags include both), so if you need an answer in java:
String input = "/data/name/data/name";
char search = '/';
long count = input.chars().filter(ch -> ch == search).count();
(and if you need a kotlin version, just take a look at #Ivo's answer)
I think you can count with this way,
val str = "/data/name/data/name"
var count = 0
str.forEach {
if(it == '/'){
count++
}
}

Lowercase first letter and use "_" instead of " " in Java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a Java programm and I would like to make every character of a word in a String a lowercase character and replace an empty space " " by a "_". So here are some expample:
"Tall Building" --> "tall_building"
"Red Shoes" --> "red_shoes"
"Water" --> "water"
I do not want to use any libarary if that is possible. Would you mind telling me how I can do that? I'd appreciate every comment.
public static String modifyString(String str) {
if (str == null)
return null;
if (str.isEmpty())
return str;
StringBuilder buf = new StringBuilder(str.length());
for(int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
buf.append(ch == ' ' ? '_' : Character.toLowerCase(ch));
}
return buf.toString();
}
P.S. Sure it could be more another solutions e.g. like str.replace(" ", "_").toLowerCase(). My solution uses StringBuilder which is correct way buil String and does not use Regexp. Time complexity is O(n).

check regex for integers bigger than the negative -328 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm looking for a regex that is able to match numbers bigger than -328, and if it is possible to provide another solution to match the same pattern but without the zero. I tried many things but still not sure about how it works, for example, ^\-?[0-9]\d{3,}$
I'm using it with the com.jfoenix.validation.RegexValidator in order to check the pattern in a textfield.
Thanks
Try this.
String pat = "^-(32[0-7]|3[0-1]\\d|[1-2]\\d\\d|\\d{1,2})|\\d+$";
for (int i = -1000; i <= 1000; ++i) {
String s = Integer.toString(i);
boolean result = s.matches(pat);
if (result != (i > -328))
System.out.println(i + " fail!");
}

How tho check in Java if there is same characters in a String? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I've got a 6 char long string, and I would like to check, if there is just one from each character. How to?
You could compare the number of distinct characters to the length of the string:
boolean charMoreThanOnce = s.chars().distinct().count() < s.length();
You can do it using a Set. You need unique elements and Set gurantees you containing the unique elements. HashSet is implementation of Set, you can use it to implement this idea.
public boolean ifAllCharsUnique(String input){
char[] arr = input.toCharArray();
int length = arr.length;
Set<Character> checker = new HashSet<>();
for(int i =0 ; i < length; i++){
if(checker.contains(arr[i]){
return false;
}
checker.add(arr[i]);
}
return true;
}

Determining distance between reoccurring characters in text [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to determine the distance between the same letters in a block of text and then store this value.
ex. "the following sentence is my example." for the letter "E" 11,3,3,5,6. and for "one queen? it would be 3,1. Spaces will be omitted.
Sort of like associating the sentence with a number-line and getting the distance between points. I would like to do this in Java.
String (indexOf)
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(int,%20int)
Pseudo-code
String text = "...", searchText="E";
int index = 0, arrIndex = 0;
text = text.toLowerCase();
text = text.replace(" ", "");
searchText = searchText.toLowerCase();
int results[] = new int[50];
int prevMatch = text.indexOf(searchText, index);
while(prevMatch != -1) {
if ((index = text.indexOf(searchText, prevMatch+1)) == -1) {
break;
}
results[arrIndex++] = index - prevMatch;
prevMatch = index;
}
I suggest you to use split() method from String class and extract the substrings text length to check the distance. You will need also to trim() the substrings to remove the spaces.

Categories