मैं एक वर्ण से स्ट्रिंग का एक भाग निकालना चाहता हूं, जो है:
स्रोत स्ट्रिंग:
manchester united (with nice players)
लक्ष्य स्ट्रिंग:
manchester united
जवाबों:
इसे करने के कई तरीके हैं। यदि आपके पास स्ट्रिंग है जिसे आप बदलना चाहते हैं तो आप कक्षा के तरीकों replaceया replaceAllतरीकों का उपयोग कर सकते हैं String। यदि आप एक विकल्प को प्रतिस्थापित करना चाह रहे हैं तो आप निम्नलिखित का उपयोग करके सबस्ट्रिंग प्राप्त कर सकते हैंsubstring एपीआई ।
उदाहरण के लिए
String str = "manchester united (with nice players)";
System.out.println(str.replace("(with nice players)", ""));
int index = str.indexOf("(");
System.out.println(str.substring(0, index));
"()" का उपयोग करने के लिए आप सामग्री का स्थान ले सकते हैं:
int startIndex = str.indexOf("(");
int endIndex = str.indexOf(")");
String replacement = "I AM JUST A REPLACEMENT";
String toBeReplaced = str.substring(startIndex + 1, endIndex);
System.out.println(str.replace(toBeReplaced, replacement));
String toBeReplaced = str.substring(startIndex , endIndex + 1);
स्ट्रिंग बदलें
String s = "manchester united (with nice players)";
s = s.replace(" (with nice players)", "");
संपादित करें:
सूचकांक द्वारा
s = s.substring(0, s.indexOf("(") - 1);
s = s.replace(...)
स्ट्रिंग का उपयोग करें। जगह ():
http://www.daniweb.com/software-development/java/threads/73139
उदाहरण:
String original = "manchester united (with nice players)";
String newString = original.replace(" (with nice players)","");
originalString.replaceFirst("[(].*?[)]", "");
https://ideone.com/jsZhSC
replaceFirst()द्वारा प्रतिस्थापित किया जा सकता हैreplaceAll()
StringBuilder का उपयोग करके , आप निम्न तरीके से प्रतिस्थापित कर सकते हैं।
StringBuilder str = new StringBuilder("manchester united (with nice players)");
int startIdx = str.indexOf("(");
int endIdx = str.indexOf(")");
str.replace(++startIdx, endIdx, "");
मैं पहली बार एक स्ट्रिंग के साथ स्ट्रिंग के एक सरणी में मूल स्ट्रिंग को विभाजित करूंगा "(" और आउटपुट सरणी के स्थिति 0 पर स्ट्रिंग वह है जो आप करना चाहते हैं।
String[] output = originalString.split(" (");
String result = output[0];
आपको स्ट्रिंग ऑब्जेक्ट की सबस्ट्रिंग () विधि का उपयोग करना चाहिए।
यहाँ एक उदाहरण कोड है:
धारणा: मैं यहां यह मान रहा हूं कि आप पहले कोष्ठक तक स्ट्रिंग को पुनः प्राप्त करना चाहते हैं
String strTest = "manchester united(with nice players)";
/*Get the substring from the original string, with starting index 0, and ending index as position of th first parenthesis - 1 */
String strSub = strTest.subString(0,strTest.getIndex("(")-1);
कॉमन्स लैंग से स्ट्रिंगरिल्ट्स का उपयोग करना
एक अशक्त स्रोत स्ट्रिंग अशक्त वापस आ जाएगी। खाली ("") स्रोत स्ट्रिंग खाली स्ट्रिंग लौटाएगा। एक नल हटाने स्ट्रिंग स्रोत स्ट्रिंग वापस आ जाएगी। रिक्त ("") निकालें स्ट्रिंग स्रोत स्ट्रिंग लौटा देगी।
String str = StringUtils.remove("Test remove", "remove");
System.out.println(str);
//result will be "Test"
// Java program to remove a substring from a string
public class RemoveSubString {
public static void main(String[] args) {
String master = "1,2,3,4,5";
String to_remove="3,";
String new_string = master.replace(to_remove, "");
// the above line replaces the t_remove string with blank string in master
System.out.println(master);
System.out.println(new_string);
}
}
यदि आपको "" के बाद सब कुछ हटाने की आवश्यकता है, तो यह कोशिश करें। अगर कोई कोष्ठक नहीं है तो कुछ भी नहीं।
StringUtils.substringBefore(str, "(");
यदि अंत कोष्ठक के बाद सामग्री हो सकती है, तो यह प्रयास करें।
String toRemove = StringUtils.substringBetween(str, "(", ")");
String result = StringUtils.remove(str, "(" + toRemove + ")");
अंत रिक्त स्थान को निकालने के लिए, का उपयोग करें str.trim()
Apache StringUtils फ़ंक्शन शून्य-, खाली- और कोई मेल नहीं- सुरक्षित हैं
आप replaceअपनी स्ट्रिंग को ठीक करने के लिए उपयोग कर सकते हैं । निम्नलिखित सब कुछ एक "(" से पहले वापस कर देगा और सभी प्रमुख और अनुगामी व्हाट्सएप को भी छीन लेगा। यदि स्ट्रिंग एक "(" के साथ शुरू होती है तो वह इसे छोड़ देगा।
str = "manchester united (with nice players)"
matched = str.match(/.*(?=\()/)
str.replace(matched[0].strip) if matched
यदि आप अंत से एक विशिष्ट स्ट्रिंग निकाल रहे हैं, तो उपयोग करें removeSuffix ( प्रलेखन )
var text = "one(two"
text = text.removeSuffix("(two") // "one"
यदि प्रत्यय स्ट्रिंग में मौजूद नहीं है, तो यह मूल वापस करता है
var text = "one(three"
text = text.removeSuffix("(two") // "one(three"
यदि आप किसी पात्र के बाद हटाना चाहते हैं, तो उपयोग करें
// Each results in "one"
text = text.replaceAfter("(", "").dropLast(1) // You should check char is present before `dropLast`
// or
text = text.removeRange(text.indexOf("("), text.length)
// or
text = text.replaceRange(text.indexOf("("), text.length, "")
आप यह भी देख सकते हैं removePrefix, removeRange, removeSurrounding, और replaceAfterLastजो समान हैं
पूरी सूची यहाँ है: ( प्रलेखन )