Converting String to char Array without using String functions

Coverting String to char Array without using String functions

  • (2 Pages)
  • +
  • 1
  • 2

28 Replies - 14330 Views - Last Post: 30 August 2010 - 03:40 PM Rate Topic: -----

#1 Guest_Lokesh Rangineni*


Reputation:

Converting String to char Array without using String functions

Posted 25 August 2010 - 06:38 AM

Hi Guys,

I can convert String to char array by using str.toCharArray() method. But i want to convert String to char array without using ANY of the String methods available in java API.

I have been asked this question in one of the interveiw.

I have tried alot and searched in google as well, but not able to find the solution.

Any help will be appreciated.
Is This A Good Question/Topic? 0

Replies To: Converting String to char Array without using String functions

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,259
  • Joined: 27-December 08

Re: Converting String to char Array without using String functions

Posted 25 August 2010 - 06:44 AM

If you can't use the String class, I would pass the String to a StringBuilder and parse it with the StringBuilder.
Was This Post Helpful? 1
  • +
  • -

#3 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2021
  • View blog
  • Posts: 4,194
  • Joined: 11-December 07

Re: Converting String to char Array without using String functions

Posted 25 August 2010 - 06:54 AM

*
POPULAR

The underlying char array that makes up a String is private. Methods from the String class are provided so you can safely see the chars. It's probably possible (and easy) to set up a stream which lets you read the chars. However, the stream will use String's methods in the background.

The only other way I can think of to do it is to use reflection. It's messy and horrible, and ties your program to a particular implementation of String. Possibly tying you to a particular java version and a particular vendor. This works on my JDK:

		// myString holds "World". This is a convoluted way of doing it.
		// You'll see why in a minute.
		String myString = "Hello World!".substring(6, 11);
		System.out.println(myString);

		// Use reflection to get at the char array field
		Field value = String.class.getDeclaredField("value");
		// It's private so we need to explicitly make it accessible.
		value.setAccessible(true);
		char[] values = (char[])value.get(myString);
		// Print them to the screen. There is a problem. We can't just rely on
		// the char array, we need the offset and length too!
		System.out.println(Arrays.toString(values));	
		
		// Get the private fields offset and count in the same way as above.
		Field offsetField = String.class.getDeclaredField("offset");
		offsetField.setAccessible(true);
		int offset = offsetField.getInt(myString);
		Field countField = String.class.getDeclaredField("count");
		countField.setAccessible(true);
		int count = countField.getInt(myString);
		
		// Extract the myString's contents from the array returned above.
		char[] properValues = Arrays.copyOfRange(values, offset, count+offset);
		System.out.println(Arrays.toString(properValues));	


Was This Post Helpful? 5
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,259
  • Joined: 27-December 08

Re: Converting String to char Array without using String functions

Posted 25 August 2010 - 06:56 AM

+1 for a demonstration of reflection and an interesting solution. Just my opinion though, but I'd rather parse the String using another tool. :)
Was This Post Helpful? 0
  • +
  • -

#5 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2021
  • View blog
  • Posts: 4,194
  • Joined: 11-December 07

Re: Converting String to char Array without using String functions

Posted 25 August 2010 - 06:59 AM

Certainly. Reflection is maybe the worst way to handle Strings. However, good luck finding a tool that manipulates Strings without using String's methods. ;)
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,259
  • Joined: 27-December 08

Re: Converting String to char Array without using String functions

Posted 25 August 2010 - 07:00 AM

View Postmacosxnerd101, on 25 August 2010 - 09:44 AM, said:

If you can't use the String class, I would pass the String to a StringBuilder and parse it with the StringBuilder.

StringBuilder. If the requirement wasn't for individual characters, I'd suggest Scanner as well. :)
Was This Post Helpful? 0
  • +
  • -

#7 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2021
  • View blog
  • Posts: 4,194
  • Joined: 11-December 07

Re: Converting String to char Array without using String functions

Posted 25 August 2010 - 07:12 AM

Creating a StringBuilder like new StringBuilder("Hello World!") calls the constructor. The constructor calls the append(String) method which in turn calls the superclass' append(String) method. Source below:

387     public AbstractStringBuilder   append(String   str) {
388     if (str == null) str = "null";
389         int len = str.length();
390     if (len == 0) return this;
391     int newCount = count + len;
392     if (newCount > value.length)
393         expandCapacity(newCount);
394     str.getChars(0, len, value, count);
395     count = newCount;
396     return this;
397     }


Read more: http://kickjava.com/src/java/lang/AbstractStringBuilder.java.htm#ixzz0xcvCBgk9


I count two of String's methods being used:
str.length()
str.getChars(0, len, value, count)
Was This Post Helpful? 3
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,259
  • Joined: 27-December 08

Re: Converting String to char Array without using String functions

Posted 25 August 2010 - 07:18 AM

I guess unless other tools using String class methods is an issue, then you are dead on with reflection. Good show. :^:
Was This Post Helpful? 0
  • +
  • -

#9 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2021
  • View blog
  • Posts: 4,194
  • Joined: 11-December 07

Re: Converting String to char Array without using String functions

Posted 25 August 2010 - 07:24 AM

You know, I wonder what they were getting at with that question. Equally valid answers would be:

Don't be so fucking stupid. String is designed to allow access to its chars through methods. Anything else compromises design.

Use a StringBuilder or stream.

Use reflection.
Was This Post Helpful? 1
  • +
  • -

#10 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,259
  • Joined: 27-December 08

Re: Converting String to char Array without using String functions

Posted 25 August 2010 - 07:28 AM

If it was an interview question, it was probably a test to see if a candidate could think creatively and find alternative solutions. In which case, I fault not the question nor the interviewer.
Was This Post Helpful? 0
  • +
  • -

#11 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2021
  • View blog
  • Posts: 4,194
  • Joined: 11-December 07

Re: Converting String to char Array without using String functions

Posted 25 August 2010 - 07:35 AM

Most probably. :) I bet there were bonus marks for pointing out it was a bad idea though. :)
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Converting String to char Array without using String functions

Posted 25 August 2010 - 04:20 PM

Really a theorical question, you can access all classes but not the methods in String:
You can write it to a file and read it back one byte at a time.
Was This Post Helpful? 2
  • +
  • -

#13 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,033
  • Joined: 15-July 08

Re: Converting String to char Array without using String functions

Posted 25 August 2010 - 05:35 PM

I love this thread! It really makes me learn many many things. cfoley, how did you know about that StringBuffer thing? And where do you get the code!?
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,259
  • Joined: 27-December 08

Re: Converting String to char Array without using String functions

Posted 25 August 2010 - 05:37 PM

@Dogstopper: Once you install the JDK, you can go into the program files and there should be a src.zip file in the subdirectory for Java somewhere. You can go through the standard JDK tools in there.
Was This Post Helpful? 1
  • +
  • -

#15 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Converting String to char Array without using String functions

Posted 25 August 2010 - 05:46 PM

And if you want to examine some more reflection, sun has some good examples HERE
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2