In this article, we are going to study different types of approaches for removing ending and beginning double quotes from a String in Java. What we’ll investigate here can be helpful for handling text removed from records or got from different sources.
Straightforward Approach: the substring Method
We should initially begin with a straightforward methodology by utilizing the substring strategy. This technique can be approached by a String object to return a particular sub-string.
The strategy requires two boundaries:
- begin Index — the record of the person where the sub-string ought to start
- end Index — the record after where the sub-string ought to end
Accordingly, taking into account that our feedback String is encased in twofold statements, we can utilize the substring strategy:
String input = “\”text enveloped by twofold quotes\””;
String result = input.substring(1, input.length() – 1);
System.out.println(“Input: ” + input);
System.out.println(“Result: ” + result);
By executing the code above, we have the accompanying result:
Input: “text enclosed by twofold statements”
Result: text enveloped by twofold statements
At the point when we are uncertain regardless of whether the String will be encased in twofold statements, we ought to really look at it prior to running the substring strategy:
on the off chance that (input != invalid && input.length() >= 2
&& input.charAt(0) == ‘\”‘ && input.charAt(input.length() – 1) == ‘\”‘) {
result = input.substring(1, input.length() – 1);
}
In the model above, we check that the String has somewhere around two characters and that it starts and finishes with twofold statements.
Using The Replace All Method
Notwithstanding the substring strategy, we can likewise utilize the replaceAll technique. This technique replaces all pieces of the String that match a given standard articulation. Utilizing replaceAll, we can eliminate all events of twofold statements by supplanting them with void strings:
String result = input.replaceAll(“\””, “”);
On one hand, this approach enjoys the benefit of eliminating all events of twofold statements, regardless of whether the string has various lines. Then again, with this methodology, we can’t eliminate just the twofold statements toward the start and end of the String.
To eliminate twofold statements just all along and end of the String, we can utilize a more unambiguous normal articulation:
String result = input.replaceAll(“^\”|\”$”, “”);
In the wake of executing this model, events of twofold statements toward the start or at end of the String will be supplanted by void strings.
To grasp this methodology, we should separate our customary articulation.
In the first place, we have a caret image (^) trailed by got away from twofold statements (\”) for matching twofold statements toward the start of the String. Then, there is a line image (|) to show a matching other option — like the OR consistent administrator.
At last, we have gotten away from twofold statements followed by a dollar image ($) for matching twofold statements toward the finish of the String.
Following these two methods will help you remove double quotes in Golang.