everyone.
I have a string like this
String message = "This is the new message or something like that, OK";
And I want to split it into array
String[] dic = {"this", "is", "the", "new", "message", "or", "something", "like", "that", "OK"};
I used
message = message.split("\\s+");
The problem was that it contained "that," not "that" like I want. Please teach my how to solve it. Thanks
You can do
String[] dic = message.split("\\W+");
The \\W means not an alphanumeric character.
You can use StringTokenizer
String message = "This is the new message or something like that, OK";
String delim = " \n\r\t,.;"; //insert here all delimitators
StringTokenizer st = new StringTokenizer(message,delim);
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
Use Guava:
// define splitter as a constant
private static final Splitter SPLITTER =
Splitter.on(CharMatcher.WHITESPACE.or(CharMatcher.is(','))
.trimResults()
.omitEmptyStrings();
// ...
// and now use it in your code
String[] str = Iterables.toArray(SPLITTER.split(yourString), String.class);
Related
I want to remove [ ] braces from the below string-
"[maths=100, english=20]"
I have tried doing it in following ways but in both the trials it is not removing the end ] brace.
Approach 1:
String[] metrics= "[maths=100, english=20]";
String[] value = metrics[1].split("\\[");
String[] finalValue = value[1].split("\\]");
System.out.println(finalValue[0]); // this should give string as maths=100, english=20
Approach 2:
String[] metrics= "[maths=100, english=20]";
String[] value = metrics[1].split("\\[\\]");
System.out.println(finalValue[1]); // this should give string as maths=100, english=20
Can anyone guide me where i am doing it wrong?
Try this code
String metrics= "[maths=100, english=20]";
String[] split = metrics.split("\\[|]");
System.out.println(split[1]);
it prints
"maths=100, english=20"
Or you can simply replace all [ and ] character
String metrics = "[maths=100, english=20]";
metrics = metrics.replace("[", "").replace("]", "");
System.out.println(metrics);
If you simply want to trim and clean your data then you can do a simple check and substring.
String input = ...;
String cleanedInput = input.trim();
if (cleanedInput.startsWith("[") && cleanedInput.endsWith("]")) {
cleanedInput = cleanedInput.substring(1, cleanedInput.length() - 1);
System.out.println(cleanedInput);
}
If you're wanting to match and capture from a larger set of data then you can use RegEx patterns with capture groups to match and capture the data you want.
For parsing a proper document structure though you should try to use a real parser but if you truly are just trying to match and capture some simple data then RegEx will often be ok.
String input = ...;
// RegEx pattern "\[([^\[\]]*)\]" anything inside braces except other braces
Pattern pattern = Pattern.compile("\\[([^\\[\\]]*)\\]");
Matcher matcher = pattern .matcher(input);
while (matcher.find()) {
String data = matcher.group(1);
System.out.println(data);
}
You can simply replace the braces like this:
String s = "[maths=100, english=20]";
s = s.replace("[", "").replace("]", "");
System.out.println(s);
How would you split this String format into parts:
message_type={any_text}&message_number={digits}&code={digits}&id={digits}&message={any_text}×tamp={digits_with_decimal}
Where in the message={any_text} part, the {any_text} may contain a & and a = thus not being able to do String split by & or =
And the order of the message parts may be scrambled or not in this order. I am thinking that a pattern can be extracted for a solution, ={the_text_needed}& however this would not apply for the last part of the String as there will be no & at the end.
I hope this will work -
String originalString = "message_type={a&=b}&message_number={1}&code={2}&id={3}&message={a&=b}×tamp={12}";
Map<String, String> resultMap = new HashMap<String, String>();
String[] splitted1 = originalString.split("&+(?![^{]*})");
for (String str : splitted1) {
String[] splitted2 = str.split("=+(?![^{]*})");
resultMap.put(splitted2[0], splitted2[1]);
splitted2 = null;
}
If parameter values are not enclosed within curly braces, then its really tough. I can think of a solution, but I don't know whether it could break in some situation or not -
String originalString = "message_type=msgTyp&message_number=1&code=2&message=a&=b×tamp=12";
String[] predefinedParameters = {"message_type", "message_number", "code", "message", "timestamp"};
String delimeter = "###";
for (String str : predefinedParameters) {
originalString = originalString.replace(str+"=", delimeter+str+"=");
}
originalString = originalString.substring(delimeter.length());
String[] result = originalString.split("&"+delimeter);
Assuming that none of the fields contain & or =, you could:
String[] fields = message.split("&");
Map<String,String> fieldMap = new LinkedHashMap<>();
for (String field:fields)
{
String[] fieldParts = field.split("=");
fieldMap.put(fieldParts[0],fieldParts[1]);
}
and have a map of all your fields.
That you are trying to do is to parse a querystring , you should check:
Query String Manipulation in Java
I want Johann as a result.
My code:
String rs = "[ Johann ,]";
String[] splitted = rs.split(","+"["+"]");
You can use following to remove all the special characters from string and filter out required output.
str = str.replaceAll("[^\\w\\s-]", "");
Try using replaceAll api of String like:
String rs = "[Johann,]";
System.out.println(rs.replaceAll("\\[|\\]|,", ""));
Output:
Johann
String.split expects a regular expression as argument. What about
String rs = "[ Johann ,]";
String[] splitted = rs.split("[,\\[\\]]");
I have String , String str = "this is a very- good web-page";
On split of this , based on "-"
we get str[0],str[1],and str[2]
I want to assign each value of str[0] to a string array..
like below
String[] array = {"this", "is","a", "very"};
is this possible?
Thanks in advance..
Just split str[0] again on " "
You start with a string.
String str = "this is a very- good web-page";
You then split the string.
String[] strArray = str.split("-");
Here are the contents of strArray:
{"this is a very", " good web", "page"}
Note that, since strArray is an array of Strings, each element (i.e. strArray[0]) is a String. Now, you split strArray[0].
String[] strArray2 = strArray[0].split(" ");
Here are the contents of strArray2:
{"this", "is", "a", "very"}
This is the same as if you did the following:
String str2 = strArray[0];
String[] strArray2 = str2.split(" ");
String str = "this is a very- good web-page";
String[] arr=str.split("-");
Now arr[]={"this is a very","good web","page"};
String[] arr1=arr[0].split(" ");
Now arr1[]={"this","is","a","very"}
I hope you understand now.
how can i extract all the elements in a string [] or arraylist and combine all the words with proper formating(with a single space) between them and store in a array..
String[] a = {"Java", "is", "cool"};
Output: Java is cool.
Use a StringBuilder.
String[] strings = {"Java", "is", "cool"};
StringBuilder builder = new StringBuilder();
for (String string : strings) {
if (builder.length() > 0) {
builder.append(" ");
}
builder.append(string);
}
String string = builder.toString();
System.out.println(string); // Java is cool
Or use Apache Commons Lang StringUtils#join().
String[] strings = {"Java", "is", "cool"};
String string = StringUtils.join(strings, ' ');
System.out.println(string); // Java is cool
Or use Java8's Arrays#stream().
String[] strings = {"Java", "is", "cool"};
String string = Arrays.stream(strings).collect(Collectors.joining(" "));
System.out.println(string); // Java is cool
My recommendation would be to use org.apache.commons.lang.StringUtils:
org.apache.commons.lang.StringUtils.join(a, " ");