How to use method String.split

I am spliting a string, just like "1a|24|3|4". My code is
String[] array = new String[10];
String buff = "1|2|3|4";
array = buff.split("|");
System.out.println(array[1]);
I think that the result should be "24", but the result always
is "1". Why? When I use String.split(",") to split a string "1a,24,3,4",
I can get the result "24". Can't "|" be used as a delimiter? Who can
explain this issue? Thanks.

Hi
The argument of split is a "regular expression" instead any common string with delimiters as used in StringTokenizer.
The char "|" is named as "branching operator" or "alternation"..doesn't matter this moment.
If you must to use "|" in source strings, change the regular expression to "\\D", it means "any non numeric char":
// can use any delimiter not in range '0'..'9'
array = buff.split("\\D");
Regards.

Similar Messages

  • Problem using java String.split() method

    Hi all, I have a problem regarding the usage of java regular expressions. I want to use the String.split() method to tokenize a string of text. Now the problem is that the delimiter which is to be used is determined only at runtime and it can itself be a string, and may contain the characters like *, ?, + etc. which are treated as regular expression constructs.
    Is there a way to tell the regular expression that it should not treat certain characters as quantifiers but the whole string should be treated as a delimiter? I know one solution is to use the StringTokenizer class but it's a legacy class & its use is not recommended in Javadocs. So, does there exist a way to get the above functionality using regular expressions.
    Please do respond if anyone has any idea. Thanx
    Hamid

    public class StringSplit {
    public static void main(String args[]) throws Exception{
    new StringSplit().doit();
    public void doit() {
    String s3 = "Dear <TitleNo> ABC Letter Details";
    String[] temp = s3.split("<>");
    dump(temp);
    public void dump(String []s) {
    System.out.println("------------");
    for (int i = 0 ; i < s.length ; i++) {
    System.out.println(s);
    System.out.println("------------");
    Want to extract only string between <>
    for example to extract <TitleNo> only.
    any suggestions please ?

  • How to use methods when objects stored in a linked list?

    Hi friend:
    I stored a series of objects of certain class inside a linked list. When I get one of them out of the list, I want to use the instance method associated with this object. However, the complier only allow me to treat this object as general object, ( of Object Class), as a result I can't use instance methods which are associated with this object. Can someone tell me how to use the methods associated with the objects which are stored inside a linked list?
    Here is my code:
    import java.util.*;
    public class QueueW
         LinkedList qList;
         public QueueW(Collection s) //Constructor of QuequW Class
              qList = new LinkedList(s); //Declare an object linked list
         public void addWaiting(WaitingList w)
         boolean result = qList.isEmpty(); //true if list contains no elements
              if (result)
              qList.addFirst(w);
              else
              qList.add(w);
         public int printCid()
         Object d = qList.getFirst(); // the complier doesn't allow me to treat d as a object of waitingList class
              int n = d.getCid(); // so I use "Object d"
         return n; // yet object can't use getCid() method, so I got error in "int n = d.getCid()"
         public void removeWaiting(WaitingList w)
         qList.removeFirst();
    class WaitingList
    int cusmNo;
    String cusmName;
    int cid;
    private static int id_count = 0;
         /* constructor */
         public WaitingList(int c, String cN)
         cusmNo = c;
         cusmName = cN;
         cid = ++id_count;
         public int getCid() //this is the method I want to use
         return cid;

    Use casting. In other words, cat the object taken from the collection to the correct type and call your method.
       HashMap map = /* ... */;
       map.put(someKey, myObject);
       ((MyClass)(map.get(someKey)).myMethod();Chuck

  • How to use method POST to send XML using utl_http?

    Hi,
    I am trying to work out how to use the POST method to mimic submitting data from a HTML form, using utl_http.
    I cannot use SOAP for this as the people I am sending the data to are just expecting an XML document to be sent to a standard CGI script running on their web server.
    There is a lot of documentation on how to submit data using the POST method (using Utl_Http.Write_Text etc), but I cannot find anything that tells me how to submit form variables.
    The examples on OTN say this:
    "Alternatively use method => 'POST' and Utl_Http.Write_Text to
    build an arbitrarily long message"
    but don't tell me how to actually place the data into the required input parameters of the CGI program I am hitting.
    I need to hit a URL such as
    www.asite.com/cgiprogram
    and pass in a parameter, eg
    in_param="test+data"
    I am currently doing this as a GET, which works, but the XML document I am passing in could potentially be bigger than the GET method will allow.
    Any help would be greatly appreciated.
    Thanks,
    Leon.

    Please try the PL/SQl forum

  • How to use method setXSLT

    Hi All!
    how I can use method setXSLT from OracleXMLQuery class
    when I try:
    java.lang.NoClassDefFoundError: oracle/xdb/XMLType
         oracle.xml.sql.query.OracleXMLQuery.setXSLT(OracleXMLQuery.java:747)
    code:
    xmlQuery = new OracleXMLQuery(this.mainDbConn.getConn(),sql);
    xmlQuery.setXSLT("/[path]/[name].xsl","");

    Specify stylesheet as a URI.
    xmlQuery.setXSLT("file://c:/util.xslt");this don't work :(

  • How to use subset string on read visa

    Hi everybody,
    please help me, i have problem about how to pick some string on String indicator (Display Hex)
    please see pict below :
    look at read buffer indicator, how to pick them one by one (Volt, Ampere, Watt, kVar, Cosphi) ? i wanna convert it to decimal. please help me
    THANKS~
    Attachments:
    Modbus NI VISAA.vi ‏13 KB
    Modbus NI VISAA.vi ‏13 KB

    Just use String Subset to pull out parts you need.  For voltage, you will want index to be 2 with a length of 4.  You can then do the conversion however you need to.
    There are only two ways to tell somebody thanks: Kudos and Marked Solutions

  • How to Use Methods AFTER Work Item Execution (Modal Call)

    Hi,
    Need to execute a piece of code after a decision based on the result.
    Hope this can be done using Methods After WorkItem Execution
    Can anyone give some idea about how to use this.
    Regards
    Imman

    Hi Mike,
    I have a common piece of code that has to get executed irrespective of the decision made,but after the decision.
    Imman

  • How too use the string tokeniser class to format date strings

    Using the code below I want to write a method which takes a string as a parameter and process it as follows:
    Input: 21/07/62
    Output: 21st July 62
    I wish to do this using the string tokenizer class. Can anyone help??
    import java.io.*;
    public class Input
    public static void main(String args[]) throws IOException
    String theString;
    BufferedReader stdin = new BufferedReader(
    new InputStreamReader(System.in));
    System.out.println("Enter your string now please");
    theString = stdin.readLine(); // throws IOException
    System.out.println("You entered ***" + theString + "***");
    }

    You can certainly use a StringTokenizer to parse your date into three numbers but I think you should use SimpleDateFormat, which will even help you generate your desired format: http://java.sun.com/j2se/1.4/docs/api/java/text/SimpleDateFormat.html

  • How to use List String in JSP page?

    Hello All,
    I am having problem using List<String> in my JSP page. Below is my JSP code.
    <%@ page import="java.util.*, java.io.*"%>
    <html>
    <body>
    <h1 align="center">Beer Recommendations JSP</h1>
    <p>
    <%
    List<String> beerBrands = (List<String>)request.getAttribute("styles");
    Iterator<String> it = beerBrands.iterator();
    while(it.hasNext()){
         out.print("<br>try: " + it.next());
    %>
    </body>
    </html>
    When I compile the above JSP code in Eclipse 3.4 (using JBoss 4.2 as my Application Server), I get the following Warning.
    Type safety: Unchecked cast from Enumeration to Enumeration<String>
    If I add the "@SuppressWarnings("unchecked")" to the code, Eclipse does not give any Error during compilation. But, at runtime, I get the following Error.
    org.apache.jasper.JasperException: Unable to compile class for JSP:
    An error occurred at line: 10 in the jsp file: /BeerAdvice.jsp
    Syntax error, annotations are only available if source level is 5.0
    7: <p>
    8:
    9: <%
    10: @SuppressWarnings("unchecked")
    11: List<String> beerBrands = (List<String>)request.getAttribute("styles");
    12: Iterator<String> it = beerBrands.iterator();
    13: while(it.hasNext()){
    An error occurred at line: 11 in the jsp file: /BeerAdvice.jsp
    The type List is not generic; it cannot be parameterized with arguments <String>
    8:
    9: <%
    10: @SuppressWarnings("unchecked")
    11: List<String> beerBrands = (List<String>)request.getAttribute("styles");
    12: Iterator<String> it = beerBrands.iterator();
    13: while(it.hasNext()){
    14:      out.print("<br>try: " + it.next());
    An error occurred at line: 11 in the jsp file: /BeerAdvice.jsp
    Syntax error, parameterized types are only available if source level is 5.0
    8:
    9: <%
    10: @SuppressWarnings("unchecked")
    11: List<String> beerBrands = (List<String>)request.getAttribute("styles");
    12: Iterator<String> it = beerBrands.iterator();
    13: while(it.hasNext()){
    14:      out.print("<br>try: " + it.next());
    Any help is very much appreciated.
    Thank you for your help.
    Thanks,
    Chubha

    Hi anotherAikman,
    Thank you for your help. I currently have the following version of the Java.
    {color:#800000}java version "1.6.0_17"
    Java(TM) SE Runtime Environment (build 1.6.0_17-b04)
    Java HotSpot(TM) Client VM (build 14.3-b01, mixed mode, sharing)
    {color:#000000}{color:#000000}Does it mean I have Java SE Version 6 Update 17 and you are suggesting me to download the Java EE Version 6?{color} If this is correct, can you please let me know what difference does it make?
    I am now going to install the Java EE 6 and try this out.
    Thank you for your help.{color}
    {color:#000000}Thanks,
    Chubha{color}
    {color}

  • How to use method of Standard Class

    Hi all,
                  I want to call a pop up in pcui screen in some standard application with some text. I have made Zclass of  standard SAP class which i am using (cl_bsp_accmod_contact in CRM system).
    I think i have to call 'PRINT_STRING' method of  standard class CL_BSP_ELEMENT.
    <i>I am trying to use this code: </i>
    <b>
    data: lr_data type ref to cl_bsp_element.
      concatenate html 'rohit' into html.  (html is a string)
      lr_data->print_string( html ).</b>
    <u>but it gives a dump:</u>
    You attempted to use a 'NULL' object reference (points to 'noth
    access a component (variable: "lr_data").
    An object reference must point to an object (an instance of a class)
    before it can be used to access components.
    I dont know how to assign object reference to an object???
    Plz help me in this regard,
    Thanks in advance,
    Rohit

    Hi ,
    try and use :
    data: lr_data type ref to cl_bsp_element.
    concatenate html 'rohit' into html. (html is a string)
    lr_data->print_string( html = html ).
    You are creating a variable...but not passing the value to the parameter...it is exactly like callin get_data or get_cell_value...!!
    Hope this helps.
    <i><b>Do reward each useful answer.</b></i>
    Thanks,
    Tatvagna.

  • Protected scope - how to use methods with it

    I'm trying to use the method removeRange():
    http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html#removeRange(int,%20int)
    From the ArrayList class, but it won't let me as it calls protected void. Is there any way around this so I can use the method?
    Edited by: Chris123 on May 3, 2008 6:03 PM

    class Testing
      MyArrayList<String> list = new MyArrayList<String>();
      public Testing()
        for(int x = 0; x < 10; x++) list.add(""+x);
        printList();
        list.removeRange(2,5);
        printList();
      public void printList()
        for(int x = 0, y = list.size(); x < y; x++) System.out.println(list.get(x));
        System.out.println("===");
      public static void main(String[] args){new Testing();}
    class MyArrayList<E> extends java.util.ArrayList<E>
      protected void removeRange(int fromIndex,int toIndex) {super.removeRange(fromIndex,toIndex);}
    }

  • How to use methods from a JAR file inside my springcontext (Oracle fusion 12c) class file?

    Dear Friends,
    I have a jar file, which has executed classes and methods in it. I want to make use of these methods inside my springcontext piece of code.
    Can someone please share an example  of how to write spingcontext code which is accessing classes/methods from  JAR files along with the any setup?
    Thanks,

    I have found the answer... as described in:
    http://java.sun.com/javase/6/docs/technotes/guides/lang/resources.html
    the problem was that the properties are loaded with the getResource & getResourceAsStream methods and I didn't know that one. I thought that is was loaded through findClass because I saw the property files trying to be loaded through findClass.
    The truth is that it tries to load with the getRessources methods and if it fails tries with the findClass/loadClass.
    To Fix the problem, I have simply overriden the getRessourceAsStream to do my magic and that was it.
    Thanks

  • How to use method find(Object pk) in java EE 5

    I've just started programming in java ee and netbeans and I do have some startup problems. I'm going to access a database using persistence entity classes. My task is to fetch the lastname of a user in a table called User and present it in the webbrowser.
    Can anyone see any errors? It always presents Fetch from database: John
    Seems like it won't get anything from the UserTestFacade find method. Is this the right way to use it?
    Here is the code for the servlet that is going to present the info:
    public class RegisterServlet extends HttpServlet {
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    String str = initDB();
    String title = "Fetch from database: ";
    out.println("<BODY BGCOLOR=\"#FDF5E6\">\n" +
    "<H1>" + title + str + "</H1>\n" +
    "</BODY></HTML>");
    out.close();
    public String initDB()
         String str = "John";
         try { 
    UserTestFacade u = new UserTestFacade();
    UserTest us = u.find(new String("Tom"));
    str = us.getLastname();
    } // end try
    catch ( Exception e ) {
         e.printStackTrace();
    finally {
         return str;
    Here is some of the code for the sessionbean UserTestFacade:
    @Stateless
    public class UserTestFacade implements UserTestFacadeLocal, UserTestFacadeRemote {
    @PersistenceContext
    private EntityManager em;
    /** Creates a new instance of UserTestFacade */
    public UserTestFacade() {
    public void create(UserTest userTest) {
    em.persist(userTest);
    public UserTest find(String username) {
    return (UserTest) em.find(UserTest.class, username);
    Here is the code for the entityclass UserTest
    public class UserTest implements Serializable {
    @Id
    @Column(name = "Username", nullable = false)
    private String username;
    @Column(name = "Firstname", nullable = false)
    private String firstname;
    @Column(name = "Lastname", nullable = false)
    private String lastname;
    public UserTest(String username) {
    this.username = username;
    public UserTest(String username, String firstname, String lastname) {
    this.username = username;
    this.firstname = firstname;
    this.lastname = lastname;
    public String getUsername() {
    return this.username;
    }

    Hi,
    Till now this question is not answered.........
    Any Answer around this deeply apprciated.
    Edited by: DoraiRaj on Sep 29, 2009 3:55 AM

  • APEX application or page url  - How to use enum string to replace ID number

    URL to a APEX page is format as ***/f?p=APPLICATION_ID:PAGE_ID:... . Here the APPLIACTION_ID and PAGE_ID are numbers.
    My Question is: Is there a way to replace the numbers by using strings? For example: can I have a URL like /f?p=MY_APPLICATION:MY_PAGE1: ... ? Juts like ENUM in other languages, MY_APPLICATION actually just represent the application id but much more readable since it is a string with a name the user may familar.
    Please help.
    Thank you.

    You can assign an alphanumeric alias to applications and pages using their respective attribute edit forms in the Application Builder. Then you can construct links that use those aliases instead of the IDs. These aliases will generally be preserved in the URL visible in the browser, but not in all cases. You have to deliberately use the aliases in any branch definitions, list item targets, etc. Note that application aliases must be unique within a workspace. Please see the User's Guide for more info.
    Scott

  • How to use method of one class in other class.

    Hi.I am new to object orient approach .I am developing a project which has a class as application .My class has it state control as another class which is notification class.I want to use my notification class method (init)in my application class method .i have also used read and write accessors vis.But i have broken lines in my code.Please help me correct it. and print screen is attached below.
    Attachments:
    Application.docx ‏143 KB

    On the left, you have wired 2 sources on the same tunnel/wire, so that's 1 broken wire.
    In the For loop, you are trying to wire a "Red" object from the first subVI to the input of the "Green" subVI, so a "Green" object. If Red is not a descendant class of Green (or share a common ancestor), it is not permitted.
    FInally, the output of the For loop, is a 1D array of "Green" objects, which is probably not the same data type as the input of the last subVI on the right (it's probably a "Green" object scalar).
    Overall, you can use the "List Errors" dialog box (Ctrl+L) to debug your code, and make exhaustive use of the contextual help (Ctrl+H) to see what is going on when focusing a broken wire.
    Regards
    Eric M. - Application Engineering Specialist
    Certified LabVIEW Architect
    Certified LabWindows™/CVI Developer

Maybe you are looking for

  • Oracle 10g performance tuning tools

    hi, can anyone please suggest me any oracle database tuning tool to use for improving the performance of the database?(oracle 10g)

  • Login Issues in jboss.

    Hi I am facing a authentication issues in jboss.Actually i have developed an .ear application using Netbeans.And it is deployed succesfully on Glassfish,and i was able to login also.It has sun-web.xml and inside it has <security-role-mapping> <role-n

  • Calendar Shuts Down After OS4

    Okay, this is so weird I hate to even mention it. But after updating itunes and then upgrading to OS4, Calendar simply stops working and shuts down...anytime I try to open up Tuesday. Any Tuesday. Of any month. I press on a Tuesday to look at my appo

  • My ipod is stolen what can i do?

    my ipod touch is stolen, what can i do ?

  • Apple & Olympus E-M5 DNGs

    I've asked this on other forums without luck. Has anyone been able to use Adobe DNG Converter 6.7 to create DNGs from their Olympus EM-5 that work on Mac OS 10.7? For some reason, DNGs created from the sample ORFs on dpreview work just fine, but it d