• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

can we typecast String to String array

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 };
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
knowledge is the difference between drudgery and strategic action -- tiny ad
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic