How to get substring from string without split?

getting a substring from a string after a particular word in java
how to extract a substring from a string in java using regex
extract substring from string java
extract substring from string python
java substring between two strings
split the string without using split() method in java
split(string java)
how to get specific string from string in java
String str = "internet address : http://test.com Click this!";

I want to get "http://test.com", so I wrote like this.

String[] split = str.split(" ");
for ( int i = 0 ; i < split.length ; i++ ) {
    if ( split[i].contains("http://") ) {
        return split[i];
    }
}

but I think this is ineffective. how to get that more easily?

Assuming you always have the same format (some text : URL more text) this can work:

public static void main(String[] args) throws IOException {
    String str = "internet address : http://test.com Click this!";
    String first = str.substring(str.indexOf("http://"));
    String second = first.substring(0, first.indexOf(" "));
    System.out.println(second);
}

But better is regex as suggested in different answer

I want to split string without using split function?, How do I split a string into number of substrings? Method substring () returns a new string that is a substring of given string. Java String substring () method is used to get the substring of a given string based on the passed indexes. There are two variants of this method. In this guide, we will see how to use this method with the help of examples.

Usually, this is either done with a regular expression or with indexOf and substring.

With a regular expression, this can be done like that:

    // This is using a VERY simplified regular expression
    String str = "internet address : http://test.com Click this!";
    Pattern pattern = Pattern.compile("[http:|https:]+\\/\\/[\\w.]*");
    Matcher matcher = pattern.matcher(str);
    if (matcher.find()) {
        System.out.println(matcher.group(0));
    }

You can read here why it's simplified: https://mathiasbynens.be/demo/url-regex - tl;dr: the problem with URLs is they can have so many different patterns which are valid.

With split, there would be a way utilizing the URL class of Java:

   String[] split = str.split(" ");

    for (String value : split) {
        try {
            URL uri = new URL(value);
            System.out.println(value);
        } catch (MalformedURLException e) {
            // no valid url
        }
    }

You can check their validation in the OpenJDK source here.

Java split String: How to split a String into tokens with , How do you extract a substring from a string in Python? There is a simplified version of this method in case the substring is nested in between two instances of the same String: substringBetween(String str, String tag) The substringAfter method from the same class gets the substring after the first occurrence of a separator.

My try with regex

String regex = "http?:\\/\\/(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_\\+.~#?&//=]*)";
String str = "internet address : http://test.com Click this!";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
if (matcher.find()) {
    System.out.println(matcher.group(0));
}

result:

http://test.com

source: here

How to extract the substring between two markers?, Assuming you always have the same format (some text : URL more text) this can work: public static void main(String[] args) throws IOException { String str  This section shows how to use the Substring or Split Methods to get a PowerShell Substring from a string. Substring and Split methods are part of the System.String class. To get all the methods in an object use the Get-Member cmdlet. Before we dive in, let’s look at the syntax of these methods. The syntax of the substring method: string

Find the http:// in the string, then look forwards and backwards for the space:

int pos = str.indexOf("http://");
if (pos >= 0) {
  // Look backwards for space.
  int start = Math.max(0, str.lastIndexOf(' ', pos));

  // Look forwards for space.
  int end = str.indexOf(' ', pos + "http://".length());
  if (end < 0) end = str.length();

  return str.substring(start, end);
}

Get Substring from String in Java, The practical ways of using the useful substring functionality in Java - from index, but without it – substring will go all the way to the end of the String. We can use the split method from the String class to extract a substring. Excel VBA SubString. SubString is a part of the string or portion or the character of the string is called “SubString”.There are three types of substring function in VBA LEFT, RIGHT and MID they are similar to the worksheet substrings in excel.

It is not clear if the structure of the input string is constant, however, I would do something like this:

    String str = "internet address : http://test.com Click this!";
    // get the index of the first letter of an url
    int urlStart = str.indexOf("http://");
    System.out.println(urlStart);
    // get the first space after the url
    int urlEnd = str.substring(urlStart).indexOf(" ");
    System.out.println(urlEnd);
    // get the substring of the url
    String urlString = str.substring(urlStart, urlStart + urlEnd);
    System.out.println(urlString);

How to split string without using inbuilt functions, public class stringSplit { public static void main(String[] args)throws IOException { BufferedReader br=new How do you find the substring of a string? I have shared below sample code as mentioned below for splitting a string in C language. Java String substring (int beginIndex, int endIndex) example It returns a string that is a substring of the string. The substring begins with the character at the specified 'beginIndex' (included) to the 'endIndex' (excluded) position. Java program to get substring of a string from given start index to end index.

Split Method in Java: How to split a String using split() Method , This article is focused on how to split strings using the split method in Java. Listed below are the topics covered in this article: Split Method in Java; Using a split() method without the limit System.out.println( "Number of substrings: " +arrOfStr.​length); For that, we have another variant of the split() method. Console.WriteLine (authorName); Now, if you want to retrieve a substring everything after 12th position. You can simply pass the starting position in the string to get the rest of the string as a substring. The following code snippet gets a substring that has all characters after the 12th position in a string.

Split the string into substrings using delimiter, We use cookies to ensure you have the best browsing experience on our website​. By using Split the string based on the delimiter and print the list of resulting sub strings. Examples: function to split string into substrings on the. // basis of  substring(split(item()?['Name'],',')[0],0,6) I really want to extract each value between the commas (not including the file extension) and insert into a column in the SharePoint library where the file is being stored any ideas or help would be greatly received.

Parsing Strings with split, When we have a situation where strings contain multiple pieces of information (​for Strings in Java can be parsed using the split method of the String class. Re: extract substring from string field Vincent Ikeagu Apr 20, 2020 8:36 AM ( in response to Azeem Sarwar ) To extract the sub string after the first space.

Comments
  • regex seems to suit you well in this case, but must clarify the pattern carefully.
  • See this question
  • Which part of the example string is constant and which part is variable?
  • Why do you believe your code is "ineffective"? Does your code not work in certain cases? A solution that uses eg. regex can also get complex quite easily.
  • There's no need to use substring twice: use the indexOf(String, int) overload to get the end. But remember that you (may) need to handle the cast of there being no following space.
  • Wow this solution is great! I feel i'm stupid, why am I couldn't think this way? Andy Turner is great too!
  • @Warak if you unsure about the input string format - regex is the best way IMO
  • I think this solution is not perfect when string input is not end with " ". so I need to check 'second' is -1. regex is better maybe.
  • @Warak actually it looks for space after URL regardless the input string end. But sure regex is the best way, just be careful with a proper pattern
  • regex. Thank you very much!
  • @Warak I know you have your own accepted answer and I have no complaint about that. But a little notice: keep in mind the comment I did mention about but must clarify the pattern carefully, that's why I put the link in my answer for further investigation/understanding all the risk to choose which pattern suit you the most.
  • Isn't Math class heavy? is better performance than split or regex?