Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
I try this code:
byte[] data = new byte[66000];
int count = is.read(data);
String sRequest = new String(data); //will received byte array hello
String all = sRequest;
all = all.concat("world");
System.out.println(all);
It only print to my console: hello
concat funtion of java have bug? I also used + operator instead concat function but result same :(
How can I concat a String with new String from a byte array?
Instead of
String sRequest = new String(data); //will received byte array hello
use
String sRequest = new String(data, 0, count); //will received byte array hello
You will notice the difference when you additionally print the length of the result string:
System.err.println(all + "/" + all.length());
gives helloworld/66005 in the first case and helloworld/10 in the second case. The reason you only see "hello" might be an issue of your console - in Eclipse, I do see "helloworld" but when I copy&paste that into another editor only one of the words is taken. The 0 values from the initial array are part of the result (since they already had been added to the first string), but they are not printed out (since they are not printable characters).
Related
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 1 year ago.
Improve this question
I need your help with String Java Program
Input : aaabbaaeedddbbbb
Output : a3b2a2e2d3b4
by using Java 8, one interview asking me
by using Groupby,streams
You can use Matcher#replaceAll for this purpose.
String str = "aaabbaaeedddbbbb";
String res = Pattern.compile("(.)\\1*").matcher(str)
.replaceAll(mr -> mr.group(1) + mr.group().length());
System.out.println(res);
Demo
Here is a method using streams.
the regular expression splits between different characters
the map appends the string character and its length.
the collector joins the strings.
String s = "aaabbaaeedddbbbb";
String result = Arrays.stream(s.split("(?<=(.))(?!\\1)"))
.map(str->str.charAt(0)+""+str.length())
.collect(Collectors.joining());
System.out.println(result);
Prints
a3b2a2e2d3b4
Here is one way to do it using groupingBy and streams (as requested in your question). It applies the previous method of splitting the source string.
String result = Arrays.stream(s.split("(?<=(.))(?!\\1)"))
.collect(Collectors.groupingBy((a) -> "result",
Collectors.mapping(
str -> str.charAt(0) + ""
+ str.length(),
Collectors.joining())))
.get("result");
However, imo this is very kludgely and a better way may exist. But even then, it is inferior to other methods and I don't know why an interviewer would ask this.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm receiving a string of bytes from my server like so:
60,104,101,97,100,62,60,115,99,114,105,112
How can i convert this into a readable string?
This is what I've tried to do:
String s = new String(json.get("msg").toString());
System.out.println("Text Decryted : " + s);
But this just gives me:
60,104,101,97,100,62,60,115,99,114,105,112
Any ideas how to accomplish this?
Parse a byte array from it:
String byteString = "60,104,101,97,100,62,60,115,99,114,105,112";
String[] byteStrings = byteString.split(",");
byte[] bytes = new byte[byteStrings.length];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte)Integer.parseInt(byteStrings[i]);
}
Then interpret the bytes as a string in a particular character set. Find out which character set you're actually using, to avoid future problems.
String string = new String(bytes, java.nio.charset.StandardCharsets.UTF_8);
The above is not very efficient by the way. Since you're already sending a string from the server, it would make more sense just to send the actual string. If you can't send the string as-is, you should ask about that problem.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a string like this- 11/15/2013.
I want to replace 2013 with 2000 (last 2 digits with 0). how to do this?
You can always do replace date.replace("13", "00");
but if you are looking at something generic of a solution (In case if you are not aware of what the last 2 digits are, yet you want them to be '00') you can do something like this :
String date = "11/15/2013";
String newDate = date.substring(0,8)+"00";
Or you can use a StringBuilder:
StringBuilder date = new StringBuilder("11/15/2013");
date.setCharAt(8, '0');
date.setCharAt(9, '0');
or
StringBuilder date = new StringBuilder("11/15/2013");
date.replace(8, 10, "00");
code snippet
String date = "11/15/2013";
String replaced = date.replace("2013","2000");
If you really want to replace a sequence at a certain index of a String, use a StringBuilder:
String replaced = new StringBuilder(date)
.replace(8, 10, "00").toString();
String date = "11/15/2013";
System.out.println(date.substring(0, date.length()-2).concat("00"));
Is this what you are looking for?
Better code snippet
String replaced = date.replace("11/15/2013.","11/15/2000");
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
For this string, 16.82080560, 96.13055810 I want to get
"String one = 16.82080560"
and
"String two = 96.13055810"
in android.
Admin that I suck in string manipulation and regex.
Please let me know how can I get such two value from a string.
String[] components = original.split(",");
If the Strings are always separated by a comma you can use String.split
For a better regex pattern see the comment from #npinti:
Minor side note, it might be better to do \\s*,\\s* instead of just ,.
Just , might cause problems should the OP wish to cast these to
floats, since the extra white space at the beginning of the second
number will most likely not be recognized as a proper number.
Another option:
String str = "16.82080560, 96.13055810";
StringTokenizer st = new StringTokenizer(str,", ");
String one = st.nextToken();
String two = st.nextToken();
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have this xml file from where I'm reading this string,
http://localhost:8080/sdpapi/request/10/notes/611/
My question is how can I get just the 611, which is of variable, can be 100000, for example, from this string?
Split the string
String input = "http://localhost:8080/sdpapi/request/10/notes/611/";
String output = input.split("notes/")[1].split("/")[0];
output is the value you need
What language?
Anyway, in most cases it's a syntax like:
String.substring(begin, length);
... where 'begin' is the number of the letter in the string-1. For extracting http from the above string you would write
substring(0, 4);
In case you always need the last string between the last two '/'s, you can retrieve the position of the slashes with index-functions (as stated in the answer of #Liran for example).
// EDIT: In Java the second parameter of substring is not length, but endIndex:
String s = "http://localhost:8080/sdpapi/request/10/notes/611/";
s.substring(46, s.lastIndexOf('/'));
It depends on programming language you use, but Regular Expressions should be the same in most of them:
/(\d+)\/$/
well, it depend in what language are you writing... in c# for example
string s = #"http://localhost:8080/sdpapi/request/10/notes/611/";
s.SubString(s.LastIndexOf('/'));
or
Path.GetFileName(s);
for java
new File(s).getName();