How does Java return string fields
This article mainly explains how Java returns string fields. Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "Java how to return string fields"!
Title: The return string has several fields.
Idea: Simple, spaces are the flags that distinguish fields.
Write your own trim() function to remove spaces at both ends of a string;
Determine whether the string is null or not. If null, return 0, which means there are 0 fields.
Distinguish fields by using the space flag: the previous character is a space, the current character is not a space, and the segment count is incremented by one.
Language:cpp
class Solution {public://string& trim(string &s) {if (s.empty()) {return s; } s.erase(0, s.find_first_not_of(">)); s.erase(s.find_last_not_of(" ") + 1);return s; }int countSegments(string s) {//String empty, returns 0if (trim(s).empty()) {return 0; }int ans = 1; s = trim(s); for (int i = 0; i < s.length(); i++) {//a field if (s[i-1] == ' ' && s[i] != if the previous character is a space and the current character is not a space ' ') { ans++; } }return ans; }}; At this point, I believe that everyone has a deeper understanding of "how Java returns string fields", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!