जवाबों:
स्वीकृत उत्तर सही है, लेकिन यह आपको यह नहीं बताता कि इसका उपयोग कैसे करें। यह है कि आप एक साथ indexOf और सबस्ट्रिंग फ़ंक्शन का उपयोग कैसे करते हैं।
String filename = "abc.def.ghi"; // full file name
int iend = filename.indexOf("."); //this finds the first occurrence of "."
//in string thus giving you the index of where it is in the string
// Now iend can be -1, if lets say the string had no "." at all in it i.e. no "." is found.
//So check and account for it.
String subString;
if (iend != -1)
{
subString= filename.substring(0 , iend); //this will give abc
}
आप सिर्फ स्ट्रिंग विभाजित कर सकते हैं ।
public String[] split(String regex)
ध्यान दें कि java.lang.String.split सीमांकक के नियमित अभिव्यक्ति मूल्य का उपयोग करता है। मूल रूप से इस तरह ...
String filename = "abc.def.ghi"; // full file name
String[] parts = filename.split("\\."); // String array, each element is text between dots
String beforeFirstDot = parts[0]; // Text before the first dot
बेशक, यह क्लैरिटी के लिए कई लाइनों में विभाजित है। इसे इस प्रकार लिखा जा सकता है
String beforeFirstDot = filename.split("\\.")[0];
यदि आपकी परियोजना पहले से ही कॉमन्स-लैंग का उपयोग करती है , तो स्ट्रिंगटीलिल्स इस उद्देश्य के लिए एक अच्छा तरीका प्रदान करते हैं:
String filename = "abc.def.ghi";
String start = StringUtils.substringBefore(filename, "."); // returns "abc"
javadoc [2.6] [3.1] देखें
या आप कुछ इस तरह की कोशिश कर सकते हैं
"abc.def.ghi".substring(0,"abc.def.ghi".indexOf(c)-1);
def.hij.klm? तब आपका कोड कैसे काम करेगा? (आपका कोड केवल इस एक उदाहरण के लिए काम करता है - आप एक ऐसा फ़ंक्शन लिख सकते हैं जो रिटर्न करता है "abc"- यह भी ठीक काम करेगा)
रेगेक्स का उपयोग कैसे करें?
String firstWord = filename.replaceAll("\\..*","")
यह पहले डॉट से अंत तक "" के साथ सब कुछ बदल देता है (यानी यह इसे साफ करता है, जो आप चाहते हैं उसे छोड़कर)
यहाँ एक परीक्षण है:
System.out.println("abc.def.hij".replaceAll("\\..*", "");
आउटपुट:
abc
Java.lang.String में आपको indexOf () जैसे कुछ तरीके मिलते हैं: जो आपको पहली बार किसी char / string का index देता है। और lstIndexOf: जो आपको String / char का आखिरी इंडेक्स देता है
जावा डॉक्टर से:
public int indexOf(int ch)
public int indexOf(String str)
निर्दिष्ट वर्ण की पहली घटना के इस स्ट्रिंग के भीतर सूचकांक लौटाता है ।
यहाँ वह कोड है जो Stringकिसी वर्ण की दी गई सूची के किसी भी समय तक एक विकल्प देता है :
/**
* Return a substring of the given original string until the first appearance
* of any of the given characters.
* <p>
* e.g. Original "ab&cd-ef&gh"
* 1. Separators {'&', '-'}
* Result: "ab"
* 2. Separators {'~', '-'}
* Result: "ab&cd"
* 3. Separators {'~', '='}
* Result: "ab&cd-ef&gh"
*
* @param original the original string
* @param characters the separators until the substring to be considered
* @return the substring or the original string of no separator exists
*/
public static String substringFirstOf(String original, List<Character> characters) {
return characters.stream()
.map(original::indexOf)
.filter(min -> min > 0)
.reduce(Integer::min)
.map(position -> original.substring(0, position))
.orElse(original);
}
यह मदद कर सकता है:
public static String getCorporateID(String fileName) {
String corporateId = null;
try {
corporateId = fileName.substring(0, fileName.indexOf("_"));
// System.out.println(new Date() + ": " + "Corporate:
// "+corporateId);
return corporateId;
} catch (Exception e) {
corporateId = null;
e.printStackTrace();
}
return corporateId;
}