Java first Learning-take the substring, find the string, go to the space commonly used Java to remove space 1. String.Trim () trim () is removed from the end of the space 2.str.replace ("", ""); Remove all methods to explain __java

Source: Internet
Author: User
Remove spaces in Java

1. String.Trim ()
Trim () is to remove the trailing space

2.str.replace ("", ""); Remove all spaces, including end to end, middle
String str = "Hell o";
String str2 = Str.replaceall ("", "");
System.out.println (STR2);

3. or ReplaceAll ("+", ""); Remove all spaces

4.str =. ReplaceAll ("\\s*", "");
can replace most whitespace characters, not limited to spaces
\s can match spaces, tabs, page breaks, and any of the white space characters.


5. Or the following code can also remove all spaces, including the end, the middle

public string Remove (String Resource,char ch)
{
    stringbuffer buffer=new stringbuffer ();
    int position=0;
    char Currentchar;
    while (Position<resource.length ())
    {
        Currentchar=resource.charat (position++);
        if (currentchar!=ch) buffer.append (Currentchar); return buffer.tostring ();
}

The full code for the test is as follows: Public

class Test1 {public

static void Main (string[] args) {

    String qjstr = "Hello WCh"; 
  
   string QJstr1 = Remove (Qjstr, ');

    System.out.println (qjstr + "\ n" + QJstr1);

}

public static string Remove (String Resource,char ch)

{

    stringbuffer buffer=new stringbuffer ();

    int position=0;

    char Currentchar;

    while (Position<resource.length ())

    {

        Currentchar=resource.charat (position++);

        if (currentchar!=ch) buffer.append (Currentchar);

    }

    return buffer.tostring ();

}


  

/////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////

There are four ways to find the substring of strings in Java, as follows:
1, int indexOf (string str): Returns the index of the first occurrence of the specified substring in this string.
2, int indexOf (String str, int startIndex): Returns the index of the first occurrence of the specified substring in this string, starting at the specified index.
3, int lastindexof (string str): Returns the index of the specified substring that appears at the far right of this string.
4, int lastindexof (String str, int startIndex): Begins a backward search from the specified index, returning the index of the specified substring that last occurred in this string.

indexof () Usage instructions
IndexOf ()

Returns the character position for the first occurrence of a substring within a string object.

String.IndexOf (substring[, StartIndex])

Parameters
String

Required option. A String object or text.

subString required option.

The substring to find in the string object.

Starindex can be selected.

An integer value that indicates the index in which to start the lookup within a String object. If omitted, it is looked up from the beginning of the string.

Description
The IndexOf method returns an integer value that indicates the starting position of the substring of the string object. If no substring is found, returns-1.

If the startindex is a negative number, the startindex is treated as zero. If it is larger than the maximum character position index, it is considered to be the largest possible index.

Performs a lookup from left to right. Otherwise, the method is the same as LastIndexOf.

Example
The following example illustrates the use of the IndexOf method.

function Indexdemo (str2) {
var str1 = "Babebibobubabebibobu"
var s = str1.indexof (STR2);
return (s);
}

Common method for string in public class Firstdemo { 

  /** 
    *api 
    * 
  //Find the specified string exists public 
  static void Main (string[) args) { 
    String str1 = "ABCDEFGHIJKLMNABC"; 
    Starts from scratch to find whether the specified character 
    System.out.println (Str1.indexof ("C")) exists; 
    Continue looking for 
    System.out.println (Str1.indexof ("C", 3) from the fourth character position); 
    If there is no such character in the specified string, the system returns-1 
    System.out.println (Str1.indexof ("X")); 
  

/////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.