Hi I am splitting and storing string with use of array but does not give result
String str = "123456";
String[] arrOfStr = str.split("");
String otpnum1 = arrOfStr[0];
String otpnum2 = arrOfStr[1];
String otpnum3 = arrOfStr[2];
String otpnum4 = arrOfStr[3];
String otpnum5 = arrOfStr[4];
String otpnum6 = arrOfStr[5];
System.out.println("otp"+otpnum1+otpnum2+otpnum3+otpnum4+otpnum5+otpnum6);
OUTPUT
System.out: otp12345
You are printing without any space or newline, which is the reason you are not able to interpret individual variables. Use this
System.out.println("otp " + otpnum1+ " " + otpnum2+" " + " "+ otpnum3+ " " + otpnum4+ " " + otpnum5+ " " + otpnum6);
I understand, the output is 12345, and expected 123456 for the result.
But, looking your code looks like correct.
I have try your code here, for test, and works fine.
The output was: otp123456
Related
I have below java string as command output
String output = "NIC Value\n"
+ "------ -----\n"
+ "vmn0 on \n"
+ "vmn1 on \n"
+ "vmn2 on \n"
+ "vmn3 on \n"
+ "vmn4 on";
I want to remove second line with dash from above string. How can I do it?
I tried it using contains method but it is generating blank line after removing second line.
if(output!=null && output.contains("-"))
output = output.replace("-","");
This is complete answer you are looking for:
String output = "NIC Value\n"
+ "------ -----\n"
+ "vmn0 on \n"
+ "vmn1 on \n"
+ "vmn2 on \n"
+ "vmn3 on \n"
+ "vmn4 on";
String str = Stream.of(output.split("\n"))
.filter(s -> !s.contains("--"))
.collect(Collectors.joining("\n"));
You can use this to remove that line and use the result,
String result = output.replace("------ -----\n", "");
It will replace that line with an empty String
I have a string from which I need to remove all mentioned punctuations and spaces. My code looks as follows:
String s = "s[film] fever(normal) curse;";
String[] spart = s.split("[,/?:;\\[\\]\"{}()\\-_+*=|<>!`~##$%^&\\s+]");
System.out.println("spart[0]: " + spart[0]);
System.out.println("spart[1]: " + spart[1]);
System.out.println("spart[2]: " + spart[2]);
System.out.println("spart[3]: " + spart[3]);
System.out.println("spart[4]: " + spart[4]);
But, I am getting some elements which are blank. The output is:
spart[0]: s
spart[1]: film
spart[2]:
spart[3]: fever
spart[4]: normal
My desired output is:
spart[0]: s
spart[1]: film
spart[2]: fever
spart[3]: normal
spart[4]: curse
Try with this:
public static void main(String[] args) {
String s = "s[film] fever(normal) curse;";
String[] spart = s.split("[,/?:;\\[\\]\"{}()\\-_+*=|<>!`~##$%^&\\s]+");
for (String string : spart) {
System.out.println("'"+string+"'");
}
}
output:
's'
'film'
'fever'
'normal'
'curse'
I believe it is because you have a Greedy quantifier for space at the end there. I think you would have to use an escape sequence for the plus sign too.
String spart = s.replaceAll( "\\W", " " ).split(" +");
Can somebody please point me to the right direction?
I am receiving a datagram packet, which I store to a string.
When I compare that string, to another one, it will result in false.
Here is the code:
private static String KEY = "Iamastring";
String sentence = new String( receivePacket.getData(), "UTF-8"); //Reconstruct the string
sentence.trim();
System.out.println("RECEIVED: " + sentence + " test " + KEY + " test ");
System.out.println("Length: " + sentence.length() + " " + KEY.equals(sentence));
And this is my output:
RECEIVED: Iamastring
Length: 1024 false
The expected comparison of KEY.equals(sentence) is true.
Strings aren't mutable :p
Therefore you must assign sentence.trim() to a variable, then use that variable for the comparison and the sysouts
sentence=sentence.trim();
If you expect trim() to help you, you must do
sentence = sentence.trim();
because trim() itself does not modify original string.
I have constructed a JSON String this way , but cannot able to pass dynamic values to it
String input = "{\r\n" +
" \"Level\": 0,\r\n" +
" \"Name\": \"String\",\r\n" +
" \"msgName\": \"String\",\r\n" +
" \"ActualMessage\": \"String\",\r\n" +
" \"TimeStamp\": \"/Date(-62135596800000-0000)/\"\r\n" +
"}" ;
String message = "this is value want to pass to the ActualMessage attribute " ;
I need to pass dynamic value to the ActaulMessage atribute
Please tell me how ?
i have tried number of trial and errors but couldn't able to succeed.
Use string concatenation.
String message = "this is value want to pass to the ActualMessage attribute " ;
String input = "{\r\n" +
"\"Level\": 0,\r\n" +
"\"Name\": \"String\",\r\n" +
"\"msgName\": \"String\",\r\n" +
"\"ActualMessage\": \"" + message + "\",\r\n" +
"\"TimeStamp\": \"/Date(-62135596800000-0000)/\"\r\n" +
"}" ;
How about using String.format() for this? for example, to pass a "dynamic value" declare a place holder in the text:
String input = "insert %s in the string"; // here %s is the placeholder
input = String.format(input, "value"); // replace %s with actual value
Now input will contain the string "insert value in the string". In your example, change this line:
" \"msgName\": \"String\",\r\n"
Replace it with this:
" \"msgName\": \"%s\",\r\n"
Now you can perform the substitution:
input = String.format(input, message);
Notice that the first parameter in the format() method has a lot more of options, and that you can pass more than one argument to be replaced. Take a look at the documentation for the Formatter class.
if you want to manipulate Json please consider GSON. your problem can be addressed as follows.
String input = "{\r\n" +
" \"Level\": 0,\r\n" +
" \"Name\": \"String\",\r\n" +
" \"msgName\": \"MessageName\",\r\n" +
" \"ActualMessage\": \"%s\",\r\n" +
" \"TimeStamp\": \"/Date(-62135596800000-0000)/\"\r\n" +
"}" ;
String message = "this is value want to pass to the ActualMessage attribute " ;
String output=String.format(input,message);
//this will replace %s with the content of message variable.
I know its very easy to split data in strings, but still i want guide to concate string,
my data is in the format. In my string the data is in the above format
104
inNetStandardGuest
windowsGuest
uestToolsTooOld
121
slesGuest
guestToolsTooOld
20569355609
Expected Output:
104,inNetStandardGuest,windowsGuest,uestToolsTooOld
121,slesGuest,guestToolsTooOld,20569355609
It's simply splitting and combining strings.
StringBuilder out = new StringBuilder();
for (String set : data.split("\n\n\n")) {
for (String line : set.split("\n")) {
out.append(line).append(',');
}
out.setCharAt(out.length(), '\n');
}
System.out.println(out);
With Guava's Splitter and Joiner:
final Iterable<String> lines = Splitter.on("\n\n\n").split(input);
for (final String line : lines) {
final Iterable<String> fields = Splitter.on("\n").split(line);
final String joined = Joiner.on(",").join(fields);
}
How about this?
String s = "104\n" +
"inNetStandardGuest\n" +
"windowsGuest\n" +
"uestToolsTooOld\n" +
"\n" +
"\n" +
"121\n" +
"slesGuest\n" +
"guestToolsTooOld\n" +
"20569355609\n";
System.out.println(s.replaceAll("(.)\\n","$1,")
.replaceAll(",,","\n")
.replaceAll(",\\n","\n"));
Probably not the most efficient way, though.
Buffered reader:
http://docs.oracle.com/javase/1.4.2/docs/api/java/io/BufferedReader.html
readLine() method:
http://docs.oracle.com/javase/1.4.2/docs/api/java/io/BufferedReader.html#readLine()
For example you read 4 lines
string outputLine = line1 + "," + line2 + "," + line3 + "," + line4;
Then read 2 lines and skip it.
If you don't know how to implement it using my advices, you should read
some basics tutorial.
Try this :
String str = "104\ninNetStandardGuest\nwindowsGuest\nuestToolsTooOld\n\n\n121\nslesGuest\nguestToolsTooOld\n20569355609";
str= str.replaceAll("\\s", ",").replaceAll(",,,", "\n");
System.out.println(str);
Output :
104,inNetStandardGuest,windowsGuest,uestToolsTooOld
121,slesGuest,guestToolsTooOld,20569355609