• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can we typecast String to String array

 
kalai sundar
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my program i use String returval="";

whether i can typecast as String array?

for eg im mrogarm i am doing as follows

int len = rtvalue.length();
String[] newArr = new String[len];
newArr = (String) returval;

i am geting error vat should i do ? how to typecast string to stringarray

help me

kalai
 
Marilyn de Queiroz
Sheriff
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A String is an object. A String array is an Array object that holds Strings. A String array is not a String and a String is not a String array. They are two different objects.

Think of an array as a container. It is holding objects of the type you've declared. For example a String array can hold Strings.
 
KasiMurugan Ramasamy
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi kalai,

You can't type cast from String to String array.

If you want create a String array from String means, you can use the following

String[] strArray = new String{ str1 };
 
Daniel Prene
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you need to do this? Explain how your going to apply it and then maybe I can lead you to an alternative.
 
kalai sundar
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my problrm is first i will read text file, in that text file i want to count the occurance of given word,(given word in the sense, i will get the word from user).if that word occur in the text file, i want to replace that word with someother word.
till now vat i have done in my program is, i read a text file, so the content of the text file is string, i was trying to string to string[], so that i can put the string array in loop, and fine for the occurence of the string, can u suggest someother idea

with rgds
kalai
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you actually don't want an array with *one* string, but an array that in every string contains one word of the original string?
 
Amol Fuke
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
If you just want to find and replace the word with another then you can use replace(word1,word2) method of String/StringBuffer.That wud be easy.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic