aspose file tools*
The moose likes Beginning Java and the fly likes Converting Char array to String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Converting Char array to String" Watch "Converting Char array to String" New topic
Author

Converting Char array to String

Santosh Kum
Greenhorn

Joined: Nov 27, 2007
Posts: 2
Hi everybody,

One way of converting a char array to string is the following way.

char data[] = {'a', 'b', 'c'};
String str = new String(data);

However, i have a scenario as follows
I donot know the number of elements in the char array.

char data[]=new char[10];

data[0]='a';
data[1]='b';
data[2]='c';
data[3]='d';data[4]='e';data[5]='f';


String str= new String(data);
System.out.println(str);

The output displayed is
abcdef[[[[ . I would like to get rid of the null characters at the end. How should i declare the char array.


The following are incorrect ways
1) char data[];
data[0]='a'

2) char data[]=new char[];

Can someone suggest me how to declare a char array without any fixed length.

Thanks
Santosh

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18990
    
  40


The String class does have a constructor that lets you specify a portion of the char array to use.

Can someone suggest me how to declare a char array without any fixed length.


Not possible. Java doesn't support variable size arrays. For that, take a look at using one of the collection classes.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Santosh Kum
Greenhorn

Joined: Nov 27, 2007
Posts: 2
Thanks for the quick reply.

But creates problem for my program.
My task is to replace a word in a String.(Without using inbuilt functions like split or using Tokenizer).
Can anybody provide any hints how to do this.

I tried using a character array but it is becoming messy as converting a char array to string is proving a bit of problem.







Snippet of my awkward code


String str="I am having breakfast";
String replacableword="breakfast";
String newword="dinner";
//String newstring="";

// The output after replacing the word is stored in a char array called newstring
char newstring []=new char[40];
char arrayofwords[][]=new char[30][40];
int i=0;
int w=0;
int len=str.length();



a: while(i<len)
{

if((str.charAt(i))==' ')
{
i++;
}

int l=0;
while(str.charAt(i)!=' ')
{


arrayofwords[w][l]=str.charAt(i);
i++;l++;

if(i==len)
{
w++;
break a;
}


}
w++;

}




Thanks
Santosh >
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Santosh Kum wrote:
char data[]=new char[10];

data[0]='a';
data[1]='b';
data[2]='c';
data[3]='d';data[4]='e';data[5]='f';


String str= new String(data);
System.out.println(str);

The output displayed is
abcdef[[[[ . I would like to get rid of the null characters at the end. How should i declare the char array.



String str = new String(data, 0, 6);

(Note that Mr. Wong pointed you in this direction.)

bitguru blog
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
You cannot replace part of a String; Strings are immutable and have no methods allowing you to do that. You can use the replace/replaceAll methods to create a new String with the alterations, but the best thing to do for Strings you wish to alter is to use StringBuilder.

I won't tell you about using reflection because this is the beginners' forum.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Converting Char array to String