java - How to find the first character of a String without using any API method -


recently being asked question in interview:

find first character of string without using method string class

gave following approaches, assuming str string:

  1. str.charat(0)
  2. str.tochararray()[0]
  3. str.substring(0,1)

can suggest me way can achieved?

using matcher api (and not string): create pattern captures every character find first 1 , print (the dotall mode enabled handle case first character line separator).

public static void main(string[] args) {     matcher matcher = pattern.compile("(.)", pattern.dotall).matcher("foobar");     if (matcher.find()) {         system.out.println(matcher.group(1)); // prints "f"     } } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -