Removing periods from strings inside tables. easy. - 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 this string I got from a table via 'string.split(" ");' the problem I have is one of them should include a period. How would I go about searching for this period. I do know I have to search for it because I code in Lua. Though we used String.find() methods.
What is the way I would remove a period from a string in a String[] table?
Thanks in advance :)
This is for school.

If I understand you correctly, you have a String that you've split by delimiting on a space. You now want to search for a period in the resultant array of Strings.
for(String s : stringArray) {
if (s.contains(".")) {
//do something
}
}

Unfortunately, I'm not that clever with RegExp, but you could...
String s = "This.is.a.test";
while (s.contains(".")) {
s = s.replace(".", "");
}
System.out.println(s);
I'm sure there's a wonderful single line RegExp to do the same thing
Updated based on comments
Because String is not mutable, you will need to reassign it back to the original array
String[] apples = {"one", "two.", "three"};
for (int index = 0; index < apples.length; index++) {
String s = apples[index];
while (s.contains(".")) {
s = s.replace(".", "");
}
apples[index] = s;
}
for (String s : apples) {
System.out.println(s);
}

Related

Replacing string for only one time 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 1 year ago.
Improve this question
Saying that there are String A = "aabbccdd" and String B = "abcd",
is there any way to remove the matching characters of String B towards String A for only one time?
Expected output is A = "abcd".
I know it will solve the problem when using for loops, but is there any simpler way to do it?
For example, using replaceAll or regular expressions?
you can use distinct() method
public static void main(String[] args) {
String str = "aabbccdd";
String result = str.chars().distinct().boxed()
.map(c -> (char) (c.intValue()))
.map(String::valueOf)
.collect(Collectors.joining());
System.out.println(result);
}
you can use the regex for that
A = A.replaceAll("([a-z]+)\1","");
can find out more about regex here https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

Create test which add country code in a loop [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 5 years ago.
Improve this question
I need to create a test which will have base URL (https://www.horisen.com) and added part with country code (i.e https://www.horisen.com/se). Problem for me is there are 12 countries to be changed.
I tried to create if loop without any success.
So, in summary, I have to go through all 12 different languages, open those pages in current languages, and continue with next lang. I suppose that I need an array of 12 country codes and call that in a loop, but do not know how to acchieve this.
Thank you in advance
String url = htps://www.horisen.com/
int[] array;
array = new int[6];
array[0] = de;
array[1] = se;
array[2] = dk;
array[3] = fr;
array[4] = en;
array[5] = fi;
for(int i=0; i++) {
do not know how to add after string url country code on the ned
}
Next time, please put some actual java code (that compiles) in your example. Perhaps you're looking for something like this:
String url = "https://www.horisen.com/";
String[] countryCodes = {"de", "se", "dk", "fr", "en", "fi"};
for (String countryCode : countryCodes) {
String countryUrl = url + countryCode;
System.out.println(countryUrl);
}

how to remove multiple characters by their indice from a string 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 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.

How to check if a string contains only specifc characters using regex [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 years ago.
Improve this question
I have a long string of digits in Java. I want to find of the string contains any one of the digits 0,1,4,6,8,9 or not. I do not want to check if my string contains just any random digits or not.
I don't care how many times the digit is present. If any one of the above digits is present in the string even once, I want to stop right there and return true.
what regex can be used to match this string?
Is there any faster way to do it instead of using regex?
I am using Java 8.
EDIT: I already found lots solutions online for checking if a string contains digits or not. Those solutions don't work here because I want to optimally find out if my string (of length~10^15 characters) contains specific digits or not.
You can use the pattern .*[014689].* along with String.matches():
String input = "1 Hello World";
if (input.matches(".*[014689].*")) {
System.out.println("Match!");
}
Assuming your String is so very big that you have to read it from an InputStream, I'd advise something of the likes :
public static final Pattern TO_MATCH = Pattern.compile("[014689]");
public static boolean hasDigits(InputStream is, int bufferSize){
BufferedReader l_read = new BufferedReader(new InputStreamReader(is, Charset.defaultCharset()),bufferSize);
return l_read.lines().filter(s -> TO_MATCH.matcher(s).find()).findAny().isPresent();
}
Where you can tweak buffersize for performance.
if (preg_match('/[^|]/', $string)) {
// string contains characters other than |
}
or:
if (strlen(str_replace('|', '', $string)) > 0) {
// string contains characters other than |
}

How to convert ArrayList of Strings to ints, so they can be added? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am trying to convert each String in an ArrayList to int so then I can add them,
asuma is an ArrayList containing String
I need to iterate over this ArrayList to convert String to int
for (int i = 0; i <=asuma.size(); i++) {
and then I need to add the integers. How do I do it?
I suspect that the ArrayList you are referring to is asuma and that it is an ArrayList<String>. In that case, you can do this:
for (String s : asuma) {
int valorFinal = Integer.parseInt(s);
}
I think u try that:
for (int i = 0; i <=asuma.size(); i++) {
int valorFinal = Integer.parseInt(asuma.get(i));
}

Categories