0

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..

3
  • 1
    yes. It is possible. Now you can try and come up with your try if you stuck some where. Commented Sep 17, 2014 at 12:34
  • so...you hust want to split the result Strings again on " "? Commented Sep 17, 2014 at 12:37
  • Why not {"this","is","a","very"} Commented Sep 17, 2014 at 12:37

3 Answers 3

2

Just split str[0] again on " "

Sign up to request clarification or add additional context in comments.

3 Comments

I dont understand. If you split one string, you get the array of Strings. Then just take the element you're interested and split it again. If str[0] is an array, what are you asking for? how assing array to another array?
String str = "this is a very- good web-page"; String[] str1 = str.split("-"); System.out.println("str1"+str1[0]); String strpart = str1[0]; String[] str2 = strpart.split(" "); System.out.println("str2--->"+str2[0]); System.out.println("str2--->"+str2[1]); System.out.println("str2--->"+str2[2]); System.out.println("str2--->"+str2[3]);
output is : str1this is a very str2--->this str2--->is str2--->a str2--->very
0

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

Comments

0

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.

2 Comments

Explaining your code would make your answer more useful than plain code.
it will give arr1[0]..etc values..but how to assign each single value to String array ...String[] a = {"this","is","a","very"} ??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.