32

I have a string and I'm getting value through a html form so when I get the value it comes in a URL so I want to remove all the characters before the specific charater which is = and I also want to remove this character. I only want to save the value that comes after = because I need to fetch that value from the variable..

EDIT : I need to remove the = too since I'm trying to get the characters/value in string after it...

ItamarG3
  • 4,092
  • 6
  • 31
  • 44
Shariq Musharaf
  • 997
  • 2
  • 10
  • 25

6 Answers6

68

You can use .substring():

String s = "the text=text";
String s1 = s.substring(s.indexOf("=") + 1);
s1.trim();

then s1 contains everything after = in the original string.

s1.trim()

.trim() removes spaces before the first character (which isn't a whitespace, such as letters, numbers etc.) of a string (leading spaces) and also removes spaces after the last character (trailing spaces).

ItamarG3
  • 4,092
  • 6
  • 31
  • 44
  • 2
    If there are multiple `=` you can get the index of the last occurrence with `s.lastIndexOf("=")`. – Irina S. May 18 '22 at 11:58
  • Just to add, if anyone wants to get the text before =, you can use this: String s1 = s.substring(0, s.indexOf("=")); then s1 contains everything before = in the original string. – Kevin May 24 '22 at 21:51
8

While there are many answers. Here is a regex example

String test = "eo21jüdjüqw=realString";
test = test.replaceAll(".+=", "");
System.out.println(test);

// prints realString

Explanation:

.+ matches any character (except for line terminators)
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
= matches the character = literally (case sensitive)

This is also a shady copy paste from https://regex101.com/ where you can try regex out.

Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
6

You can split the string from the = and separate in to array and take the second value of the array which you specify as after the = sign For example:

String CurrentString = "Fruit = they taste good"; String[] separated = CurrentString.split("="); separated[0]; // this will contain "Fruit" separated[1]; //this will contain "they teste good"

then separated[1] contains everything after = in the original string.

Arjun Mehta
  • 100
  • 1
  • 8
3

I know this is asked about Java but this seems to also be the first search result for Kotlin so you should know that Kotlin has the String.substringAfter(delimiter: String, missingDelimiterValue: String = this) extension for this case.

Its implementation is:

val index = indexOf(delimiter)
return if (index == -1) 
    missingDelimiterValue 
else 
    substring(index + delimiter.length, length)
Sir Codesalot
  • 7,045
  • 2
  • 50
  • 56
2

If you use the Apache Commons Lang3 library, you can also use the substringAfter method of the StringUtils utility class.

Official documentation is here.

Examples:

String value = StringUtils.substringAfter("key=value", "=");   

// in this case where a space is in the value (e.g. read from a file instead of a query params)
String value = StringUtils.trimToEmpty(StringUtils.substringAfter("key = value", "="));   // = "value"
    

It manage the case where your values can contains the '=' character as it takes the first occurence.

If you have keys and values also containing '=' character it will not work (but the other methods as well); in the URL query params, such a character should be escaped anyway.

рüффп
  • 5,172
  • 34
  • 67
  • 113
1

Maybe locate the first occurrence of the character in the URL String. For Example:

String URL = "http://test.net/demo_form.asp?name1=stringTest";

int index = URL.indexOf("=");

Then, split the String based on an index

String Result = URL.substring(index+1); //index+1 to skip =

String Result now contains the value: stringTest

yogur
  • 810
  • 10
  • 19