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 have a url which is generate on the fly and i want to place some text with unknowing text using string builder. Please let me know how?
Example:-
http://localhost/abcdef/servlet/cpd.abcd.build.coupons.CouponValueFormatterServlet?dsn=frd_abcdef&lang=ENG&val=PRCTXT|ABCDE_-1223344&classGroupid=101,201&fgcolor=000000&bgcolor=E0DBD8&width=100&height=80&fontSize=11&fontWeight=normal.
The above URL is a string builder and "val=PRCTXT|ABCDE_-1223344" text has to change with "val=123456" text. But here Val is always user input . so it is changing always.
If you absolute want to use StringBuilder, you should read the javadoc to find usable methods for your purpose.
That would be:
indexOf(String str)
indexOf(String str, int fromIndex)
replace(int start, int end, String str)
StringBuilder buf = new StringBuilder("http://localhost/abcdef/servlet/cpd.abcd.build.coupons.CouponValueFormatterServlet?dsn=frd_abcdef&lang=ENG&val=PRCTXT|ABCDE_-1223344&classGroupid=101,201&fgcolor=000000&bgcolor=E0DBD8&width=100&height=80&fontSize=11&fontWeight=normal.");
int start = buf.indexOf("&val=");
if (start != -1) {
start += 5;
int end = buf.indexOf("&", start);
if (end == -1)
end = buf.length();
buf.replace(start, end, "123456");
System.out.println(buf);
}
Related
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 3 years ago.
Improve this question
I am trying below code to convert string into Int but it is showing exception
String ID = "37f6c80-2d5-44c-82f-0001a40cf4"
int boardid = Integer.parseInt(ID);
Exception is :
java.lang.NumberFormatException: For input string:
I can handle exception by writing it in try and catch, but the problem is there any way I can convert it into int?
You could use Long object type but not int for this data:
public Long numberify(String number) {
Long result = Long.parseLong(number.replaceAll("[a-zA-Z|-]", ""));
return result;
}
You can get rid of all the non-number characters with this regex pattern ID = ID.replaceAll("\\D+",""); and you can then convert it into int with your int boardid = Integer.parseInt(ID);. I would probably go for long though.
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'm trying to split a String at every Nth occurence, but missing the last values.Here is what is expected.
Input : String str = "234-236-456-567-678-675-453-564";
Output :
234-236-456
567-678-675
453-564
Here N=3, where the str should be split at every 3rd occurence of -.
Try this.
String str = "234-236-456-567-678-675-453-564";
String[] f = str.split("(?<=\\G.*-.*-.*)-");
System.out.println(Arrays.toString(f));
result:
[234-236-456, 567-678-675, 453-564]
You can try the following with Java 8:
String str = "234-236-456-567-678-675-453-564";
Lists.partition(Lists.newArrayList(str.split("-")), 3)
.stream().map(strings -> strings.stream().collect(Collectors.joining("-")))
.forEach(System.out::println);
Output:
234-236-456
567-678-675
453-564
Maybe one of the worst way without using function available in java , but good like exercise :
public static void main(String[] args){
String s = "234-236-456-567-678-675-453-564";
int nth =0;
int cont =0;
int i=0;
for(;i<s.length();i++){
if(s.charAt(i)=='-')
nth++;
if(nth == 3 || i==s.length()-1){
if(i==s.length()-1) //with this if you preveent to cut the last number
System.out.println(s.substring(cont,i+1));
else
System.out.println(s.substring(cont,i));
nth=0;
cont =i+1;
}
}
}
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
A program that takes the first two characters of a string and adds them to the front and back of the string. Which version is better?
public String front22(String str) {
if(str.length()>2) return str.substring(0,2)+str+str.substring(0,2);
return str+str+str;
}
or
public String front22(String str) {
// First figure the number of chars to take
int take = 2;
if (take > str.length()) {
take = str.length();
}
String front = str.substring(0, take);
return front + str + front;
}
The former strikes me as more elegant. The latter is easier to understand. Any other suggestions for improvement of either is more than welcome!
Issue with the first option, mainly because string is immutable. [Edit.] As #Pshemo correctly pointed out, my statement was unclear. Quoting #Pshemo, "executing same substring twice is inefficient when we can reuse result from first substring".
Use a StringBuilder.
StringBuilder sb = new StringBuilder(str);
CharSequence seq = sb.subSequence(0,2);
sb.insert(0, seq);
sb.append(seq);
return sb.toString();
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 7 years ago.
Improve this question
Looking this code
String input="I use this method";
String word=input.replaceAll(" ","/");
char buf[]=word.toCharArray();
but i want to another method to doing this?
This is the easiest way I have found to convert a string including white spaces to a char array in java.
String input = "I use this method";
char[] buf = input.toCharArray();
It looks like you are doing it right, but taking out all of the white space with the replaceAll(" ", "/")
From your question I'm assuming you want to save the string without truncating the whitespace in a char array.
If your remove the line " String word=input.replaceAll(" ","/"); " from your code.Then your code will work perfectly fine.PFB a sample code which might help you to understand this better
public static void main(String[] args)
{
String input="I use this method";
System.out.println(input.length()); //length of string before converting to char array
//String word=input.replaceAll(" ","/");
char buf[]=input.toCharArray();
System.out.println(buf.length);//length of string after converting to char array
for(int i=0;i<buf.length;i++){
System.out.print(buf[i]); /// Print the values in char array
}
}
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 a string Saxon Securitie/Logo/horse-logo.jpg_1413458235818 in format "A/B/C"
I want the result as C by removing "A/B/" from the above string and get a result
String C = "horse-logo.jpg_1413458235818"
Try:
String s = "Saxon Securitie/Logo/horse-logo.jpg_1413458235818";
String c = s.substring(s.lastIndexOf("/") + 1);
System.out.println(c);
String filePath = "Saxon Securitie/Logo/horse-logo.jpg_1413458235818";
String fileName = new File(filePath).getName();
See https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem
You can use String.lastIndexOf to do that :
String path = "Saxon Securitie/Logo/horse-logo.jpg_1413458235818";
int index = path.lastIndexOf("/");
String fileName = index == -1 ? null : path.substring(index + 1);
I'm not going to give you answer but you could easily use split function in java that you can learn about here. and at first split with space then split with /