I have the following string:
"ISL-1027
20:13:02:22:00:76"
i.e. bluetooth name and MAC address
I need MAC address on a separate string.
What is the best way to use split() in this case?
Thanks
split("\n") you can use this."\n" will be the separator here.
String str = "ISL-1027" +
"\n" +
"20:13:02:22:00:76";
String[] arr= str.split("\n");
System.out.println("Bluetooth Name: "+arr[0]);
System.out.println("MAC address: "+arr[1]);
Out put:
Bluetooth Name: ISL-1027
MAC address: 20:13:02:22:00:76
If your input String like this ISL-1027 20:13:02:22:00:76(separate by a space) use follows
String str = "ISL-1027 20:13:02:22:00:76";
String[] arr= str.split(" ");
System.out.println("Bluetooth Name: "+arr[0]);
System.out.println("MAC address: "+arr[1]);
Split matching on any white space and include the DOTALL mode switch:
split("(?s)\\s+");
The DOTALL will make the regex work despite the presence of newlines.
Depending on that how your string is formated is not sure, but the format of a mac-address is defined. I would not try to split the string and hope that index n is the correct value.
Instead I would use regulare expression to find the correct position of a string that matches the mac-address format.
Here is a litle example (not tested):
String input = "ISL-1027
20:13:02:22:00:76"
Pattern macAddrPattern = Pattern.compile("[0-9a-fA-F]{2}\:[0-9a-fA-F]{2}\:[0-9a-fA-F]{2}\:[0-9a-fA-F]{2}\:[0-9a-fA-F]{2}\:[0-9a-fA-F]{2}\");
String macAdr = parseMacAddr(input);
public String parseMacAddr(String value) {
Matcher m = macAddrPattern.matcher(value);
if (m.matches()) {
return value.substring(m.start(),m.end());
}
return null;
}
This should always work.
Related
I want split my string using regex.
String Str = " Dřevo5068Hlína5064Železo5064Obilí4895";
String reg = "(\\D+)(\\d+)(\\D+)(\\d+)(\\D+)(\\d+)(\\D+)(\\d+)";
if (Str.matches(reg)) {
String[] l = Str.split(reg);
System.out.println(Arrays.toString(l));
}
But, output is []. Where is problem?
Edit: I want split to:
Dřevo
5068
Hlína
5064
Železo
5064
Obilí
4895
Then I want get numbers from this String.
if your engine permits look-around, split using this pattern
(?<=\D)(?=\d)|(?<=\d)(?=\D)
Demo
Im using java and I have a String that I would like to parse which contains the following
Logging v0.12.4
and would like to split it into a String containing
Logging
and an integer array containing
0.12.4
where
array[i][0] = 0
and
array[i][1] = 12
and so on. I have been stuck on this for a while now.
split your string on space to get Logging and v0.12.4
remove (substring) v from v0.12.4
split 0.12.4 on dot (use split("\\.") since dot is special in regex)
you can also parse each "0" "12" "4" to integer (Integer.parseInt can be helpful).
You can use a regex or just normal String splitting
String myString = "Logging v0.12.4";
String[] parts = myString.split(" v");
// now parts[0] will be "Logging" and
// parts[1] will be "0.12.4";
Then do the same for the version part:
String[] versionParts = parts[1].split("\\.");
// versionParts[0] will be "0"
// versionParts[1] will be "12"
// versionParts[2] will be "4"
You can "convert" these to integers by using Integer.parseInt(...)
Here ya go buddy, because I'm feeling generous today:
String string = "Logging v0.12.4";
Matcher matcher = Pattern.compile("(.+?)\\s+v(.*)").matcher(string);
if (matcher.matches()) {
String name = matcher.group(1);
int[] versions = Arrays.stream(matcher.group(2).split("\\.")).mapToInt(Integer::parseInt).toArray();
}
I need help in splitting two email address which are seperated by a Delimiter 'AND'. I have issue when splitting, when the email address has got the characters'AND' in the email id. For eg, if the email address that needs to be split is something like the below. There are no whitespaces between the two email address.
'anandc#AND.comANDxyz#yahoo.co.in', and the delimiter is'AND'
In the above case, there seems to be three items extracted instead of two. Can someone please help me solve this. Thanks in Advance
You can use " AND " as delimiter.
String str="anandc#AND.com AND xyz#yahoo.co.in";
String[] emailArr=str.split(" AND ");
Or you can use following regex
String str = "anandc#AND.com AND xyz#yahoo.co.in";
Pattern p = Pattern.compile("[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*#[A-Za-z0-9]+
(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})");
Matcher matcher = p.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group(0));
}
Out put
anandc#AND.com
xyz#yahoo.co.in
Giving correct output
public class Test {
public static void main(String args[]) {
String text = "anandc#AND.com AND xyz#yahoo.co.in ";
String[] splits = text.split(" AND ");
for (int i = 0; i < splits.length; i++) {
System.out.println("data :" + splits[i]);
}
}
}
Output is
data :anandc#AND.com
data :xyz#yahoo.co.in
Use this :
String[] splits = text.split("\\s+AND\\s+");
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String)
the regular expression will be case sensitive
actually, the best is to use delimiters exression that you are sure will not be in the adress
With this string "ADACADABRA". how to extract "CADA" From string "ADACADABRA" in java.
and also how to extract the id between "/" and "?" from the link below.
http://www.youtube-nocookie.com/embed/zaaU9lJ34c5?rel=0
output should be: zaaU9lJ34c5
but should use "/" and "?" in the process.
and also how to extract the id between "/" and "?" from the link below.
http://www.youtube-nocookie.com/embed/zaaU9lJ34c5?rel=0
output should be: zaaU9lJ34c5
Should be :
String url = "http://www.youtube-nocookie.com/embed/zaaU9lJ34c5?rel=0";
String str = url.substring(url.lastIndexOf("/") + 1, url.indexOf("?"));
String s = "ADACADABRA";
String s2 = s.substring(3,7);
Here 3 specifies the beginning index, and 7 specifies the stopping point.
The string returned contains all the characters from the beginning index, up to, but not including, the ending index.
I'm not entirely sure what you mean by extract, so I've provided the code to remove it from the String, I'm not certain if this is what you want.
public static void main (String args[]){
String string = "ADACADABRA";
string = string.replace("CADA", "");
System.out.println(string);
}
This is untested but something like this may help for the youtube part:
String youtubeUrl = "http://www.youtube-nocookie.com/embed/zaaU9lJ34c5?rel=0";
String[] urlParts = youtubeUrl.split("/");
String videoId = urlParts[urlParts.length - 1];
videoId = videoId.substring(0, videoId.indexOf("?"));
Extracting CADA from the string makes no sense. You will need to specify how you have determined that CADA is the string to extract.
E.g. is it because it is the middle 4 characters? Is it because you are stripping off 3 characters each side? Are you just looking for the String "CADA"? Is it characters 3,7 of the String? Is it the first 4 of the last 7 characters of a String? Is it because it contains 2 vowels and 2 consanants? I could go on..
String regex = "CADA";
Pattern p = Pattern.compile(regex, Pattern.MULTILINE);
Matcher m = p.matcher(originalText);
while (m.find()) {
String outputThis = m.group(1);
}
Use this tool http://www.regexplanet.com/advanced/java/index.html
Probably, you don't take in account the fact of java.lang.String immutability. That's why you need to assign the result of substringing to a new variable.
Can I extract string from the phrase using split() function with subphrases as delimeters? For example I have a phrase "Mandatory string - Any string1 - Any string2". How can I extract "Any string1" with delimiters as "Mandatory string" and "[a-zA-Z]"
This is how I'm trying to extract:
String str="Mandatory string - Any string1 - Any string2";
String[] result= str.split("Mandatory\\string\\s-\\s|\\s-\\s[a-zA-Z]+");
Result of this code is
result = ["Mandatory string","ny string1","ny string2"]
But desired is:
result = ["Any string1"]
Could appreciate some help, thanks.
String[] result= str.split("Mandatory\\s(1)string\\s-\\s|\\s-\\s[a-zA-Z\\s(2)]+");
You just forgot an "s" in position(1)
and there should be a "\\s" in position(2)
try this line:
String[] result= str.split("Mandatory\\sstring\\s-\\s|\\s-\\s[a-zA-Z\\s]+");
First of all, there's a typo right here:
Mandatory\\string
This should probably read
Mandatory\\sstring
Anyway, I would either use " - " as the delimiter and get the second token:
str.split(" - ")[1] // TODO: prod version should do bounds checking etc
or use a different tool entirely, probably a regex match with the following regular expression:
"Mandatory string - (.*) - .*"
The parenthesised capture group will give you the string you're after.
Why not
String[] result = str.split(" - ");
return result.length < 2 ? "" : result[1];
If there is a definite format to your input string, just split it and then use the parts that are needed:
String[] resultArray = str.split(" - ");
String whatYouWant = resultArray[1];