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
stringwithout using methodstringclass
gave following approaches, assuming str string:
str.charat(0)str.tochararray()[0]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
Post a Comment