I am using
public class Test {
public static void main(String[] args)
{
String str = "12,23,34,65,67,87,98,78,09,31,56,98,45";
String s1[] = str.split(str,3);
for(String s:s1)
System.out.println(s);
}
}
But the output is empty.I am getting empty console.But when i print the s1 array length it is giving 2 for me.What is the wrong here.
Thanks in advance..
You will get following out put by using str.split(",",3);
12
23
34,65,67,87,98,78,09,31,56,98,45
If you want to get
12
23
34
You can try something like this
String str = "12,23,34,65,67,87,98,78,09,31,56,98,45";
String s1[] = str.split(",",3);
for(String s:s1) {
System.out.println(s.substring(0,2));
}
Your question is unclear. It appears you want to split the String for every 3 numbers. For this you can use
String[] s1 = str.split("(?<=\\G\\d+,\\d+,\\d+)");
Related
I am trying to add prefix and suffix to a particular occurrence of the word in the string in java. Can anyone help me and tell me where am i going wrong? Below is my code.
public static void main(String[] args) {
String str = "Hello world. welcome world java.";
String arr[] = str.split("[. ]");
if(str.contains("world")) {
System.out.println("PREFIX_"+str+"_SUFFIX");
}
}
output expected :
Hello PREFIX_world_SUFFIX. welcome PREFIX_world_SUFFIX java
output getting:
PREFIX_Hello world. welcome world java_SUFFIX
String replaced = str.replaceAll("world", "PREFIX_world_SUFFIX");
System.out.println(replaced);
Your code is wrong since you're not changing the str variable while calling the split() function.
Also, from what I can gather, you also want to add prefix and suffix to those words containing "world".
Like if your string is something like this: Hello worldJava! welcome to java world, you'd want to display something like this: Hello PREFIX_worldJava_SUFFIX! welcome to java PREFIX_world_SUFFIX. (Note, the previous answers wouldn't be able to do this kind of substitution).
String str = "Hello world. welcome world java.";
String[] wordArr = str.split("[. ]");
Set<String> words = new HashSet<>(Arrays.asList(wordArr));
for (String w: words) {
if(w.toLowerCase().contains("world")){
str = str.replace(w, "PREFIX_"+ w +"_SUFFIX");
}
}
System.out.println(str);
Note here that I am using java Set to parse unique words from the input string and then replacing them in the original string with the added prefix/suffix.
Just do this simply :
public class Example {
public static void main(String[] args){
String str = "Hello world. welcome world java.";
System.out.println(str.replace("world", "PREFIX_world_SUFFIX"));
}
}
Output :
Hello PREFIX_world_SUFFIX. welcome PREFIX_world_SUFFIX java.
You are doing it wrong on the print side. You can do this.
String stringToCheck = "world";
if(str.contains(stringToCheck)) {
str = str.replaceAll(stringToCheck , "PREFIX_"+stringToCheck+"_SUFFIX");
System.out.println(str);
}
I have a string (URL) like this:
"https://www9.online-convert.com/dl/web2/download-file/248f2225-7ed3-48dd-a586-ac1390bbeaab/02_Cuppy_lol.webp"
I need to extract the last part only i.e. 02_Cuppy_lol.webp.
How can I do that?
Thanks!
You can use substring() and lastIndexOf() here:
String value = completeString.substring(completeString.lastIndexOf("/") + 1);
You can split this text/url and get last part, for example:
String url = "https://www9.online-convert.com/dl/web2/download-file/248f2225-7ed3-48dd-a586-ac1390bbeaab/02_Cuppy_lol.webp";
String[] splittedUrl = url.split("/");
String lastPart = splittedUrl[splittedUrl.length()-1)];
you can use the method split().follow this example
public class Demo {
public static void main(String args[]){
String str ="https://www9.online-convert.com/dl/web2/download-file/248f2225-7ed3-48dd-a586-ac1390bbeaab/02_Cuppy_lol.webp";
String[] temp=str.split("/");
int lastIndex =temp.length-1;
String lastPart = temp[lastIndex];
System.out.println(lastPart);
}
}
Output-:
02_Cuppy_lol.webp
I have an ArrayList that contains some elements and I am using String Buffer to get in a string. But When I am print the String I am getting in unorganized line. I want to get in single or double line.
ArrayList<String> mCombinedList; // it contains some value I have already instantiate it
StringBuilder builder = new StringBuilder();
for (String singleLine : mCombinedList) {
if (builder.length() > 0) {
builder.append(" ");
}
builder.append(singleLine);
}
String string = builder.toString();
Log.e(TAG, "string builder " +string );
After Implementing this code I am getting this result in Log.
abdul jani
456
Friend User 3
721015***
Friend
**ArrayList value of mCombinedList; **
[ abdul jani
456
Friend, User 3
721015***
Friend]
During the final phase of your String build using StringBuilder, when you have collected all the strings, just call this regex to remove all the white spaces that are more than 2 which will clean up all the additional white spaces that got introduced due to empty strings in your source arraylist
String string = builder.toString().replaceAll("\\s{2,}", " ").trim();
You are adding lot of spaces by builder.append(" ");. Use of ArrayList.toString() will solve your problem.
package com.test;
import java.util.ArrayList;
public class ListToString {
public static void main(String[] args) {
ArrayList<String> mCombinedList = new ArrayList<>();
mCombinedList.add("abdul jani");
mCombinedList.add("456");
mCombinedList.add("Friend");
mCombinedList.add("User");
mCombinedList.add("3");
mCombinedList.add("721015****");
mCombinedList.add("Friend");
String string = mCombinedList.toString();
//System.out.println(string);
Log.e(TAG, "string builder " +string );
}
}
Output:
[abdul jani, 456, Friend, User, 3, 721015****, Friend]
i would like to remove a character from java string using hex code:
i am trying following code but seems to not be correct as the character isn't replaced: ÿ
String str ="test ÿ";
str.replaceAll("\\x{9F}","")
is there any thing wrong with the syntax of the hex code? Thanks.
Could you please try this:
public class AsciiHexCode {
public static void main(String[] args) {
String str = "test ÿ";
String result = str.replaceAll("[^\\x00-\\x7F]", "");
System.out.println("result : "+ result);
}
}
To mach ÿ you need \u00ff instead, as Jon mentioned.
String replaced = str.replace("\u00ff", "");
in your case.
I have a requirement that i have to get a specific portion from a String i.e if i have java.lang.String i need to get the com field from the String i have done this
public class uu {
public static void main(String[] args) {
String domain = "www.xyzw.com";
String[] strArray = domain.split("\\.");
for (String str : strArray) {
System.out.println(str);
}
}
}
it is giving me three fields separately but i need the com only ..
someone help me
String s = domain.substring(domain.lastIndexOf(".")+1);
Consider inserting error checks for the case that the string does not contain .
String domain = "www.xyzw.com";
String[] strArray = domain.split("\\.");
System.out.println(strArray[2]);
You can do:
String r = strArray[strArray.length - 1];
Or:
String r = domain.substring(domain.lastIndexOf(".") + 1);
If you work with domains you could use Google Guava InternetDoMainNames class. It is very simple
InternetDomainName.from("www.xyzw.com").publicSuffix();
How to use:
https://code.google.com/p/guava-libraries/wiki/InternetDomainNameExplained
http://koziolekweb.pl/2014/03/30/pomocna-guava-informacje-o-domenie/ (with example - translation to english is posible)