This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 5 years ago.
I have the following string:
String result = "#sequence#A:exampleA#B:exampleB";
I would like to split this string into two strings like this:
String resulta = "sequence";
String resultb = "#A:exampleA#B:exampleB";
How can I do this? I'm new to Java programming language.
Thanks!
if you want typical split, (Specific to your string), you can call substring and get the part of it like below:
String s = "#sequence#A:exampleA#B:exampleB";
String s1= s.substring(1,s.indexOf("#",1));
System.out.println(s1);
String s2= s.substring(s1.length()+1);
System.out.println(s2);
}
Edit as per your comment
String s3= s.substring(s.indexOf("#",1));
System.out.println(s3);
Try,
String result = "#sequence#A:exampleA#B:exampleB", resulta, resultb;
int splitPoint = result.indexOf('#',1);
resulta = result.substring(1, splitPoint);
resultb = result.substring(splitPoint);
System.out.println(resulta+", "+resultb);
String result = "#sequence#A:exampleA#B:exampleB";
if(result.indexOf("#") == 0)
result = result.substring(1, result.length());
String resultA = result.substring(0, result.indexOf("#"));
String resultB = result.substring(resultA.length(), result.length());
Related
This question already has answers here:
How can I remove a substring from a given String?
(14 answers)
Closed 3 years ago.
I am attempting to remove a substring from the original string
code attempted:
String details = events.event_details.value[bi];
String details2 ="";
if(details.length()>70) {
details2 = details.substring(70);
details.replaceFirst(details2, " ");
}
You do not remove it, you overwrite it with spaces...
remove is: cut it off!
replace: replace it!
details2 = details.substring(61, 70);
this gives you 10 chars
details2 = details.substring(details.length-10, details.length);
last 10
if you want 60 spaces and then the last 10, you need to create a String with 60 spaces, and use above method to extract whatever you want
maybe use a StringBuffer for this...
StringBuffer strg = new StringBuffer();
String cut = details.substring(details.length-10, details.length);
IntStream.iterate(0, i -> i++).limit(60).forEach((a) -> strg.append(" "));
strg.append(cut);
System.out.println(strg.toString());
You have to save your result from replaceFirst(...) somewhere,
as example again in details:
details = details.replaceFirst(details2, "");
You can try this way-
String details2 ="";
String result = ""
if(details.length()>70) {
details2 = details.substring(70);
result = details.replaceFirst(details2, " ");
}
System.out.println(result);
Here need a String variable where contain that replace's result
This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 4 years ago.
I have a reply message in a String and I want to split it to extract a value.
My String reply message format is:
REPLY(MSGID,15,ABC024049364194,SERVICE,10,CREATE,...);
My requirement is to get the value ABC024049364194 from the above string format.
I tried using this code, but it hasn't worked:
String[] arrOfStr = str.split(",", 5);
for (String a : arrOfStr)
System.out.println(a);
If you split the String, you will simply need to access the token at index 2.
// <TYPE>(<ARGUMENTS>)
String message = "REPLY(MSGID,15,ABC024049364194,SERVICE,10,CREATE);";
Matcher m = Pattern.compile("(\\w+)\\((.+)\\)").matcher(message);
if (m.find()) {
String type = m.group(1);
String[] arguments = m.group(2).split(",");
System.out.println(type + " = " + arguments[2]); // REPLY = ABC024049364194
}
public static void main(String[] args) {
String str = "REPLY(MSGID,15,ABC024049364194,SERVICE,10,CREATE,...)";
String code = str.split(",")[2];
System.out.println(code);
}
Will work if your code is always after 2 coma
This question already has answers here:
Java: splitting a comma-separated string but ignoring commas in quotes
(12 answers)
Closed 5 years ago.
I have a string with value - String sData = "abc|def|\"de|er\"|123"; and I will need to split it with delimiter - "|". In this case, my expected result will be
abc
def
"de|er"
123
Below is my code
String sData = "abc|def|\"de|er\"|123";
String[] aSplit = sData.split(sDelimiter);
for(String s : aSplit) {
System.out.println(s);
}
But it actually comes out the below result
abc
def
"de
er"
123
I have tried with this pattern - String sData = "abc|def|\"de\\|er\"|123"; but it's still not returning my expected result.
Any idea how can I achieve my expected result?
This worked for me:
String sData = "abc|def|\"de|er\"|123";
String[] aSplit = sData.split("\\|");
for(int i = 0; i < aSplit.length; i++) {
if(aSplit[i].startsWith("\"")) {
if(aSplit[i+1].endsWith("\"")) {
aSplit[i] = aSplit[i] + "|" + aSplit[i+1];
aSplit[i+1] = "";
}
}
}
for(String s : aSplit) {
if(!s.equals(""))
System.out.println(s);
}
The output:
abc
def
"de|er"
123
I have a problem with String.format In android I want replace { 0 } with my id.
My this code not working:
String str = "abc&id={0}";
String result = String.format(str, "myId");
I think you should use replace method instead of format.
String str = "abc&id={0}";
str.replace("{0}","myId");
you have 2 ways to do that and you are mixing them :)
1.String format:
String str = "abc&id=%s";//note the format string appender %s
String result = String.format(str, "myId");
or
2.Message Format:
String str = "abc&id={0}"; // note the index here, in this case 0
String result = MessageFormat.format(str, "myId");
You have to set your integer value as a seperate variable.
String str = "abc&id";
int myId = 001;
String result = str+myId;
try this,
String result = String.format("abc&id=%s", "myId");
edit if you want more than one id,
String.format("abc&id=%s.id2=%s", "myId1", "myId2");
The syntax you're looking for is:
String str = "abc&id=%1$d";
String result = String.format(str, id);
$d because it's a decimal.
Other use case:
String.format("More %2$s for %1$s", "Steven", "coffee");
// ==> "More coffee for Steven"
which allows you to repeat an argument any number of times, at any position.
This question already has answers here:
How to convert String to long in Java?
(10 answers)
Closed 6 years ago.
how do you convert a string into a long.
for int you
int i = 3423;
String str;
str = str.valueOf(i);
so how do you go the other way but with long.
long lg;
String Str = "1333073704000"
lg = lg.valueOf(Str);
This is a common way to do it:
long l = Long.parseLong(str);
There is also this method: Long.valueOf(str); Difference is that parseLong returns a primitive long while valueOf returns a new Long() object.
The method for converting a string to a long is Long.parseLong. Modifying your example:
String s = "1333073704000";
long l = Long.parseLong(s);
// Now l = 1333073704000
IF your input is String then I recommend you to store the String into a double and then convert the double to the long.
String str = "123.45";
Double a = Double.parseDouble(str);
long b = Math.round(a);
String s = "1";
try {
long l = Long.parseLong(s);
} catch (NumberFormatException e) {
System.out.println("NumberFormatException: " + e.getMessage());
}
You can also try following,
long lg;
String Str = "1333073704000"
lg = Long.parseLong(Str);
import org.apache.commons.lang.math.NumberUtils;
This will handle null
NumberUtils.createLong(String)
Do this:
long l = Long.parseLong(str);
However, always check that str contains digits to prevent throwing exceptions.
For instance:
String str="ABCDE";
long l = Long.parseLong(str);
would throw an exception but this
String str="1234567";
long l = Long.parseLong(str);
won't.
Use parseLong(), e.g.:
long lg = lg.parseLong("123456789123456789");