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.
Related
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++
}
}
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 am currently working on a task and I have a question about the Substring method.
For the task I need to get the first Char of a String and delete the first Char after I used it.
The Strings are names and at the end I only want to have "" an empty String left.
My approach:
String name = "Paul";
char chr = name.charAt(0);
String newName = name.substring(1);
My questions: When I am at the last char "l" and use my substring do I get "" or an error?
My questions: When I am at the last char "l" and use my substring do I
get "" or an error?
You will get a blank string. It is also mentioned in the following lines of documentation:
Throws:
IndexOutOfBoundsException - if beginIndex is negative or larger than the length of this String object.
When you have only l left in the string, its length will be 1 which is perfectly acceptable as the beginIndex. You can also verify it as follows:
public class Main {
public static void main(String[] args) {
System.out.println("l".substring(1));
}
}
To make sure that is error free, do the following.
String name = "Paul";
int nameLength = name.length();
for (int i = 0; i < nameLength; i++){
char chr = name.charAt(0);
if (i != nameLength - 1){
String newName = name.substring(1);
name = newName;
} else {
name = "";
}
}
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).
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 understand that I did a lot of hard coding that can be avoided so if anything can share their thought please go ahead:
private String hideEmailCharacters(String privateEmail){
String emailName = privateEmail.substring(0,privateEmail.indexOf("#"));
StringBuilder stringBuffer = new StringBuilder(emailName);
stringBuffer.replace(emailName.length() / 2,emailName.length(), StringUtils.repeat("*", emailName.length() / 2));
String emailProvider = privateEmail.substring(privateEmail.indexOf("#"));
return stringBuffer + emailProvider;
}
The goal is to cover for example half of the email name or cover everything after the second character with stars ** so the result from abcdv#example.com would be ab***#example.com
String processing is cool but here's a solution with Regular Expression.
(?<=.{2}).(?=[^#]*?#)
Intuition:
Ignore first 2 characters from the characters before #
Ignore # as well.
Replace each character between with *
public class TestCode {
private static String hideEmailCharacters(String privateEmail) {
return privateEmail.replaceAll("(?<=.{2}).(?=[^#]*?#)", "*");
}
public static void main(String[] args) {
System.out.println(hideEmailCharacters("abcdv#example.com"));
System.out.println(hideEmailCharacters("ra0o29ajzsdc242#example.com"));
System.out.println(hideEmailCharacters("x2helloyouthere#example.com"));
System.out.println(hideEmailCharacters("a#foo.com"));
}
}
Output:
ab***#example.com
ra*************#example.com
x2*************#example.com
a#foo.com
This piece of code will do the trick:
StringBuffer email = new StringBuffer(privateEmail)
int startIndex = email.indexOf("#")/2;
int endIndex = email.indexOf("#");
int numOfCharsToHide = endIndex - startIndex;
email.replace(startIndex, endIndex, "*".repeat(numOfCharsToHide));
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 5 years ago.
Improve this question
How can I remove multiple characters by their index from a string. I thought to use StringBuilder's deleteCharAt function. But if I delete char one by one, I will not keep track the right index.
For example :
String test = "0123456789"
int[] removingIndice = int[] {2, 4, 0};
// remove character from string
// result = "1356789"
Create a new string builder,
iterate on string, add elements to builder if its index not in the array
StringBuilder sb = new StringBuilder("");
for(int i = 0; i< test.length; i++){
if( !ArrayUtils.contains(removingIndice, i))
{
sb.append(test.charAt(i));
}
}
test = sb.toString();
String is immutable in Java, so you will need to create a new String with characters at positions 0,2,4 removed. As one option, you may use StringBuilder for that, as answered here: How to remove single character from a String
I think, you need to redesign the task:
"new string must contains all characters except ..."
Now, seems weak initial data structure and the goal.