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

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).

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++
}
}

Extract first two characters of a String in Java [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 6 years ago.
Improve this question
I got a java question which is Given a string, return the string made of its first two chars, so the String "Hello" yields "He".
If the string is shorter than length 2, return whatever there is, so "X" yields "X", and the empty string "" yields the empty string "".
Note that str.length() returns the length of a string.
public String firstTwo(String str) {
if(str.length()<2){
return str;
}
else{
return str.substring(0,2);
}
}
I'm wondering is there any other way can solve this question?
Your code looks great! If you wanted to make it shorter you could use the ternary operator:
public String firstTwo(String str) {
return str.length() < 2 ? str : str.substring(0, 2);
}

Regex for a string pattern split [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 7 years ago.
Improve this question
Have a scenario to strip the below pattern strings -
abc|hjdj|kleygag|0|0|0|0|
ghys|jkugb|0|0|0
yuubf|kluygb|tyrffv|nutgv|0|0|0|0|0|
Looking for help to get a regex in java to separate these values excluding everything from the first occurence of pipe before 0, the output should look like-
abc|hjdj|kleygag
ghyd|jkugb
yuubf|kluygb|tyrffv|nutgv
Just need regex, which has been answered and been very helpful, would just be very helpful to know just the regex and not the complete code for the reverse pattern as requested in the followup question
Is regex necessary for this? substring() gets you want you want easily.
Update
I saw a comment where you're also wanting a case where the data looks like, "0|0|0|0|abdc|ghyft|rtyu". I've modified my answer to account for that case and a case where the data could be, "0|0|0|0|abdc|ghyft|rtyu|0|0|0|"
Either way:
public static void main(String[] args) throws Exception {
List<String> strings = new ArrayList(){
{
add("abc|hjdj|kleygag|0|0|0|0|");
add("ghys|jkugb|0|0|0");
add("yuubf|kluygb|tyrffv|nutgv|0|0|0|0|0|");
add("0|0|0|0|abdc|ghyft|rtyu");
add("0|0|0|0|abdc|ghyft|rtyu|0|0|0|0|0|");
}
};
// Non Regex
System.out.println("Non Regex");
for (String string : strings) {
int startIndex = -1;
int endIndex = -1;
// Find first non zero character
for (int i = 0; i < string.length(); i++) {
if ('a' <= string.charAt(i) && string.charAt(i) <= 'z') {
startIndex = i;
break;
}
}
// Find first pipe zero |0 after startIndex
endIndex = string.indexOf("|0", startIndex);
// Determine which substring() to use based on the endIndex results
System.out.println(endIndex > -1 ? string.substring(startIndex, endIndex) : string.substring(startIndex));
}
System.out.println("");
// Regex
System.out.println("Regex");
for (String string : strings) {
System.out.println(string.replaceAll("\\|0|0\\||\\|$", ""));
}
}
Results:
Non Regex
abc|hjdj|kleygag
ghys|jkugb
yuubf|kluygb|tyrffv|nutgv
abdc|ghyft|rtyu
abdc|ghyft|rtyu
Regex
abc|hjdj|kleygag
ghys|jkugb
yuubf|kluygb|tyrffv|nutgv
abdc|ghyft|rtyu
abdc|ghyft|rtyu
You can use a regex like this:
\|0|0\||\|$
Java code:
String regex = "\\|0|0\\|\\|$";
System.out.println("abc|hjdj|kleygag|0|0|0|0|".replaceAll(regex, ""));
System.out.println("ghys|jkugb|0|0|0".replaceAll(regex, ""));
System.out.println("yuubf|kluygb|tyrffv|nutgv|0|0|0|0|0|".replaceAll(regex, ""));

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.

Reading each word from Email addresses in Java [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 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

Categories