Can we add list of strings to StringArray? - java

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.

Related

Java Split String and Combine

I would like to split a string and combine them.
String value = "1,A 2,B 3,C"
outputs
[1,2 A,B]
[1,3 A,C]
[2,3 B,C]
If I do String[] tokens = value.split("[,\\s]+");
tokens[0] = "1" tokens[1] = "A" tokens[2] = "2" tokens[3] = "B" and so on.
But then how can I combine it that becomes the output? Thank you.
You can split and combine it by doing this:
String a = value.charAt(0)+","+value.charAt(4)+" "+value.charAt(2)+","+value.charAt(6);
String b = value.charAt(0)+","+value.charAt(8)+" "+value.charAt(2)+","+value.charAt(10);
String c = value.charAt(4)+","+value.charAt(8)+" "+value.charAt(6)+","+value.charAt(10);

splitting string in java using regex

How can I split following string in two strings?
input:
00:02:05,130 --> 00:02:10,130
output:
00:02:05,130
00:02:10,130
i tried this piece of code:
String[] tokens = my.split(" --> ");
System.out.println(tokens.length);
for(String s : tokens)
System.out.println(s);
but the out put is just the first part, what is wrong?
try this
String[] arr = str.split("\\s*-->\\s*");
You could use the String split():
String str = "00:02:05,130 --> 00:02:10,130";
String[] str_array = str.split(" --> ");
String stringa = str_array[0];
String stringb = str_array[1];
You may want to have a look at the following: Split Java String into Two String using delimiter

String between two commas Java

How can i isolate a String existed between two commas?
i.e. Angelo,Marco,Nick,Brandon,Paul
I want to retrieve the name Marco. Which is the most appropriate way? Should i use regex?If yes,can anyone explain me how?
The simplest solution is to use String.split(",")
like so:
String str = "Angelo,Marco,Nick,Brandon,Paul";
String[] myStrings = str.split(",");
String marco = myStrings[1];
Try this
String s = "Angelo,Marco,Nick,Brandon,Paul";
String array[] = s.split(",");
for (int i = 0; i < array.length; i++) {
System.out.println("element "+i+" "+array[i]);
}
You can use split e.g.
String[] names = "Angelo,Marco,Nick,Brandon,Paul".split(",");
// or
List<String> names = Arrays.asList("Angelo,Marco,Nick,Brandon,Paul".split(","));
// or
for(String name: "Angelo,Marco,Nick,Brandon,Paul".split(","))
System.out.println(name);
Well lets not make it complicated and use split() and Arrays.asList() method....
String str = "Angelo,Marco,Nick,Brandon,Paul";
String[] arr = str.split(",");
List<String> alist = new ArrayList<String>(Arrays.asList(arr);
String marco = alist.get(alist.indexOf("Marco"));
Voila..... its done... !!! Marco is with u now....
split string using comma separator.
**try it**
String str = "Angelo,Marco,Nick,Brandon,Paul";
String lines[]= str.split(",");
String name = lines[1];

Java split a string by space, new line, tab, punctuation

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);

how to create a string from string array or arraylist?

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, " ");

Categories